Skip to content Skip to sidebar Skip to footer

Reconnection In Socket.io Problem In `socket.on('message',function(){})`

I have a socket.io connection to connect to server and client, It work fine. Now when I try to reconnect it on disconnect from server it get connected but then socket.on('message'

Solution 1:

I don't know node.js, but it looks like syntax error, haven't you forgot the right paratheses?

socket.on('connect', function(){
    clearInterval(socketInterval);
});
socket.on('message', function(obj)
{
    alert("meg from server");
});
socket.on('disconnect', function()
{
    socketInterval=setInterval("socket.connect()",5000);
});

Solution 2:

it would appear that the "problem" most likely is on the server side. The server has two ways to send messages to the client (emit and broadcast). If you are doing a one to one message, most people use emit. I am assuming that you built a chat server which stores the sessionIds of the client. It works fine with the initial connection because the server has the correct sessionId, but let's say connection is lost and you reestablish connection, now the server tries to send a message to the client. If your server stored the initial sessionId, say in an array, and attempts to use the original sessionId to emit a message, it will fail because reconnection causes a new sessionId to be created.

The solution in this case is to remove the previous sessionId from the array and add the new sessionId upon reconnection.

Post a Comment for "Reconnection In Socket.io Problem In `socket.on('message',function(){})`"