Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaling Socket.IO across multiple servers

I've been searching around looking for help on setting up a multi-server cluster for a Node.js Socket.IO install. This is what I am trying to do:

  • Have 1 VIP in an F5 loadbalancer, pointing to n number of Node servers running Express, and Socket.IO
  • Have client connect to that 1 VIP via io.connect and then have it filter to one the of the servers behind the loadbalancer.
  • When a message is emitted on any one of those servers, it is is sent to all users who are listening for that event and connect via the other servers.

For example - if we have Server A, Server B and Server C behind LB1 (F5), and User A is connected to Server A, User B is connected to Server B and User C is connected to Server C.

In a "chat" scenario - basically if a message is emitted from Server A to message event - Server B and C should also send the message to their connected client. I read that this is possible with using socket-io.redis, but it needs a Redis box - which server should that be install on? If all the servers are connected to the same Redis box - does this work automatically?

var io = require('socket.io')(server);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));

Any help would be greatly appreciated thanks!

like image 238
gregavola Avatar asked Jun 13 '15 18:06

gregavola


People also ask

Is Socket.IO scalable?

"The Socket.IO server currently has some problems with scaling up to more than 10K simultaneous client connections when using multiple processes and the Redis store, and the client has some issues that can cause it to open multiple connections to the same server, or not know that its connection has been severed."

How much traffic can Socket.IO handle?

Once you reboot your machine, you will now be able to happily go to 55k concurrent connections (per incoming IP).

How many concurrent connections can Websockets handle?

The theoretical limit is 65k connections per IP address but the actual limit is often more like 20k, so we use multiple addresses to connect 20k to each (50 * 20k = 1 mil).

Is Websocket heavy on server?

One HTTP request and response took a total of 282 bytes while the request and response websocket frames weighed in at a total of 54 bytes (31 bytes for the request message and 24 bytes for the response). This difference will be less significant for larger payloads however since the HTTP header size doesn't change.


1 Answers

The answer to this question is that you must set up a single Redis server that is either outside your SocketIO cluster - and have all nodes connect to it.

Then you simply add this at the top of your code and it just works magically without any issues.

var io = require('socket.io')(server);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));
like image 168
gregavola Avatar answered Sep 21 '22 12:09

gregavola