Horizontally scale socket.io with redis

穿精又带淫゛_ 提交于 2019-12-09 13:05:20

问题


I currently am creating a horizontally scalable socket.io server which looks like the following:

                 LoadBalancer (nginx)

      Proxy1      Proxy2      Proxy3      Proxy{N}

 BackEnd1   BackEnd2   BackEnd3   BackEnd4   BackEnd{N}

My question is, with socket-io redis module, can I send a message to a specific socket connected to one of the proxy servers from one of the backend servers if they are all connected to the same redis server? If so, how do I do that?


回答1:


As you wan to scale socket.io server, and you have used nginx as load balancer, do not forget to setup sticky load balancing, othersie single connection will be connected to multiple server based on load balancer pass the connection to socket.io server. So better to use sticky load balancing

With the redis socket io adapter, you can send and receive message with one or more socket.io server with help of Redis Pub/Sub implementation.

if you tell me which technology is used for Proxy and Backend, i will let you know more information on this.




回答2:


Using the socket.io-redis module all of your backend servers will share the same pool of connected users. You can emit from Backend1 and if a client is connected to Backend4 he will get the message.

The key for this working though with Socket.io is to use sticky sessions on nginx so that once I client connects, it stays on the same machine. This is because the way that socket.io starts with a WebSocket and several long polling threads, they all need to be on the same backend server to work correctly.

Instead of sticky sessions, you can change your client connection optons to use Websockets ONLY and this will remove the problems with the multiple connections to multiple servers as there will only be one connection, the single websocket. This will also make your app lose the ability to downgrade to long-polling instead of WebSockets.



来源:https://stackoverflow.com/questions/34217166/horizontally-scale-socket-io-with-redis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!