在多个服务器上扩展Socket.IO [英] Scaling Socket.IO across multiple servers
问题描述
我一直在寻找关于为Node.js Socket.IO安装设置多服务器集群的帮助。这是我想要做的:
- 在F5负载均衡器中拥有1个VIP,指向
n
运行Express的节点服务器数量和Socket.IO
- 让客户端通过
io.connect
然后将其过滤到负载平衡器后面的一个服务器。 - 当这些服务器中的任一个发送消息时,将发送给正在监听的所有用户例如 - 如果我们在LB1(F5)后面有服务器A,服务器B和服务器C,那么该事件将通过其他服务器连接。
< ),用户A连接到服务器A,用户B连接到服务器B,用户C连接到服务器C。
在聊天场景中 - 基本上如果从服务器A发送消息到消息
事件 - 服务器B和C也应该将消息发送到连接的客户端。我读到这是可以使用 socket-io.redis
,但它需要一个Redis框 - 哪个服务器应该安装?如果所有服务器都连接到相同的Redis框 - 这是否会自动工作?
var io = require('socket.io ')(服务器);
var redis = require('socket.io-redis');
io.adapter(redis({host:'localhost',port:6379}));
任何帮助都将非常感谢!
这个问题的答案是你必须设置一个单独的Redis服务器,它不在SocketIO群集之外,并且所有节点都连接到它。
然后你只需将它添加到你的代码的顶部,它只是神奇地工作没有任何问题。
var io = require('socket.io')(server);
var redis = require('socket.io-redis');
io.adapter(redis({host:'localhost',port:6379}));
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 }));
这篇关于在多个服务器上扩展Socket.IO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!