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?
source share