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:
n
number of Node servers running Express, and Socket.IOio.connect
and then have it filter to one the of the servers behind the loadbalancer.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!
"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."
Once you reboot your machine, you will now be able to happily go to 55k concurrent connections (per incoming IP).
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).
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.
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 }));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With