Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontally scale socket.io with redis

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?

like image 509
Steve P Avatar asked Dec 11 '15 06:12

Steve P


2 Answers

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.

like image 62
Hiren S. Avatar answered Nov 07 '22 12:11

Hiren S.


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.

like image 23
greg_diesel Avatar answered Nov 07 '22 12:11

greg_diesel