Socket.io uses MemoryStore by default, so all connected clients will be stored in memory, which makes it impossible (well, not quieter, but in more detail) to send and receive events from clients connected to another socket.io server.
One way to make all Socket.io servers receive all events is that all servers use REDIS Pub-Sub. So, instead of using socket.emit, you can publish redis.
redis_client = require('redis').createClient(); redis_client.publish('channelName', data);
And all socket servers subscribe to this channel through Redis and, after receiving the message, emit its clients connected to them.
redis_sub = require('redis').createClient(); redis_sub.subscribe('channelName', 'moreChannels'); redis_sub.on("message", function (channel, message) { socket.emit(channel, message); });
Difficult thing !! But wait, it turns out you do not need such code to achieve the goal. Socket.io has a RedisStore that essentially does what the code above should do so that you can write Socket.io code as you would for one server, and will still propagate to another socket.io server via Redis.
To summarize, socket.io sends messages to multiple servers using Redis as a channel instead of memory.
source share