How to handle reconnection on node teardown with multiple nodes scenario #4809
-
Hi, I was wondering if it does exists any sort of mechanism that allows to recover a client connection when a backend replica/node gets torn down. In a orchestrator-oriented environment (like with kubernetes) each socket.io replica (even with active clients) may be torn down in any moment or at least when the scaling policy decides. I noticed that this particular scenario triggers (correctly) a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @giovanni-bertoncelli, I have a very limited experience in socket.io but I'm doing some experiments with connection state recovery, using a cluster of Socket.io servers (Node.js with Haproxy), connected to a Mongodb cluster through the Mongo adapter. In my simple project, connection state recovery works only in case of a temporary disconnection between client and server: in this situation, the socket.id remains the same, socket object is marked as recovered and the messages that have been sent to the client, while it was not was connected, are correctly received upon reconnection. In any case, it's really possible that I'm not doing everything correctly. One last note: this remark is very important. The recovery of a client that has not been 'initialized' with at least one message is not supported. |
Beta Was this translation helpful? Give feedback.
If I understood well: you have clients connected to a node that get torn down (maybe due to autoscaling policies, maybe because the node becomes unhealthy).
So, now you have clients disconnected, probably getting tons of errors in the JS console (or in the UI), and not able to use your application. Am I correct?
In this scenario, if you don't need to synchronize the state of the client upon reconnection (i.e. you can live with lost messages), reconnection upon "transport close" disconnection is just a matter of configuration (see 'reconnection*' options in https://socket.io/docs/v4/client-options/. Also check disconnect docs for details about disconnect events.)
Obviously, you have to con…