简体   繁体   中英

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!

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 }));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM