How to handle socketio on multiple nodes if a node is down

Viewed 96

I am trying to set up socketio on multiple nodes for load balancing between them. I came up with something like this in socketio docs: Using multiple nodes.

It does the work perfectly for event passing through redis between multiple nodes, but the question is, for example, I have this scenario:

I have Server A and Server B up and running with a load balancer (nginx)

Client A is connected to Server A.

Client B is connected to Server B.

Is there any way to make sure if Server A gets down (node gets disconnected, or shuts down for any reason) all the clients connected to Server A should be redirected to Server B automatically as a fallback? Does socketio support it? what manual configuration I have to do to make sure this happens?

I am using Express with nginx.

1 Answers

If one of your socket.io servers goes offline while in use, its clients should (eventually) get disconnect messages. You should organize your code so your disconnect event handler can establish a new connection via your load-balancer.

I wrote "eventually." It's possible a few seconds will pass between server crash and client event delivery. You may want to set up some sort of keepalive scheme where your client sends a 'ping' event once in a while, and expects a prompt 'pong' response from your server. Then, if a ping is not answered in the allotted time, your client can reconnect, just as if it got a disconnect event.

If you have a mobile client, you'll want to test all this by starting a session on wifi, then moving the device out of wifi range so it switches to wireless.

Related