Does socket.io reconnect?

I created a simple web application that does some communication through the node.js server using socket.io.

When a user connects, node binds back information that informs the client about the signing of certain events. It works great.

But if you allow the browser to set free time, the client finishes subscribing to events twice. The subscription process works as follows:

When a user connects to node.js receives the message 'adduser'

The two key points that arise in this function are the following:

socket.on('adduser', function(data) <snip> socket.emit('nodeinfo', {node_id: NODE_ID}); socket.emit('update', {msg: 'you are connected'}); 

The socket.emit ('nodeinfo') function signals the client to subscribe to events.

So the customer has

 socket.on('nodeinfo', function(data) { ... } 

The fact is that it never says “you are connected” twice, none of the console.log () functions in “adduser” are called again on the node side. The only thing that seems to happen is node.js again emits nodeinfo (or, as the client thinks), forcing the client to re-subscribe.

Can someone explain to me how socket.io reconnects in operation and how this can happen?

+6
source share
1 answer

By default, the client tries to connect after disconnection. You can change this if you want: socket.io client

When reconnecting, the server will see a new connection, and you decide to implement the logic to identify the new connection with the previous one. Therefore, in practice - instead of implementing the adduser logic after the “connection”, do it (or not) after the client sends some information to the server.

+4
source

Source: https://habr.com/ru/post/910510/


All Articles