any workaround on multiple socket.io connection after reconnect

Showing 1-13 of 13 messages
any workaround on multiple socket.io connection after reconnect ved 11/21/11 5:00 AM
Hi guys,

I know this is known issue right now: https://github.com/LearnBoost/socket.io/issues/430

But I want to know if anybody knows any workaround to get rid of multiple connection after this as I want to avoid multiple update with this.

At client side will socket.disconnect() removes all the connections? will following code works to remove multiple connection after reconnect?

socket = io.connect(url)
socket.on('connect', function(){
if(multipleConnect) {
socket.disconnect();
socket.removeAllListeners('connect');
io.sockets = {};
socket = io.connect(url);
socket.on('connect', functionToConnect);
}
})

I am not able to test this locally as not able to reproduce it locally.

Thanks,
Ved
Re: any workaround on multiple socket.io connection after reconnect ankur 11/21/11 9:52 PM
I had a similar problem affecting my code
what i did was add the listener methods outside of 'connect' function,
now i get only one event fired

instead of this


socket = io.connect(url)
socket.on('connect', function(){

socket.on('message', function (msg) {
});
});

now i do


socket = io.connect(url)
socket.on('connect', function(){

});

socket.on('message', function (msg) {
});

it appears work right..
hope this helps

Re: any workaround on multiple socket.io connection after reconnect ankur 11/21/11 10:12 PM
i had the same problem , was getting multiple events on receiving
data.
instead of this

socket = io.connect(url);
        socket.on('connect', function () {
            socket.on('message', function (msg) {
            });
          });

i did this,

socket = io.connect(url);
        socket.on('connect', function () {
          });
 socket.on('message', function (msg) {
            });

this seems to work all right and multiple events don't seem to fire.
i think it is because previous registered listener doesn't gets
unregistered after a successful re-connection attempt

Re: any workaround on multiple socket.io connection after reconnect ved 11/21/11 11:39 PM
I am already having the second implementation and it doesn't seem to work for me, to me it looks like multiple connections gets generated on same 'socket' reference, because I have 'socket = io.connect(url) only once in my code, where socket gets assigned and like your second implementation I am adding evenlisteners to same socket independently, not inside the 'connect'. So it can't be get reassigned automatically on reconnection.

Thanks,
Ved
Re: any workaround on multiple socket.io connection after reconnect ankur 11/23/11 3:23 AM
which version of socket.io do you use .. ?

On Nov 22, 12:39 pm, Vedratna Velani <vedpatel2...@gmail.com> wrote:
> I am already having the second implementation and it doesn't seem to work
> for me, to me it looks like multiple connections gets generated on same
> 'socket' reference, because I have 'socket = io.connect(url) only once in
> my code, where socket gets assigned and like your second implementation I
> am adding evenlisteners to same socket independently, not inside the
> 'connect'. So it can't be get reassigned automatically on reconnection.
>
> Thanks,
> Ved
>
>
>
>
>
>
>

Re: any workaround on multiple socket.io connection after reconnect ved 11/25/11 12:32 AM
0.8.7 the latest one.

Thanks,
Ved
Re: any workaround on multiple socket.io connection after reconnect ankur 11/29/11 8:44 PM
Is your server under heavy load....!!

On Nov 25, 1:32 pm, Vedratna Velani <vedpatel2...@gmail.com> wrote:
> 0.8.7 the latest one.
>
> Thanks,
> Ved
>
>
>
>
>
>
>

Re: any workaround on multiple socket.io connection after reconnect ved 11/29/11 11:27 PM
yes. sometime.

Thanks,
Ved
Re: any workaround on multiple socket.io connection after reconnect ankur 12/9/11 4:19 AM
i think the issue is solved in jsonp transport . try that and
see......!!

On Nov 30, 12:27 pm, Vedratna Velani <vedpatel2...@gmail.com> wrote:
> yes. sometime.
>
> Thanks,
> Ved
>
>
>
>
>
>
>

Re: any workaround on multiple socket.io connection after reconnect ved 12/9/11 4:49 AM
overall issue is not fixed yet. And I want to use websocket where issue is still there. It is regarding opening multiple transport at the same time. I think it has been fixed by 3rd-Eden in his branch where he fixed the multiple reconnection issue. He already did pull request, so fix might come with next socket.io release. Till that as a workaround I choose to disable reconnection on socket-io-client, I am handling it outside.

Thanks,
Ved
Re: any workaround on multiple socket.io connection after reconnect ved 12/9/11 4:51 AM
To disable reconnection on socket.io-client, I made a small fix here:
https://github.com/LearnBoost/socket.io-client/pull/346

Thanks,
Ved
Re: any workaround on multiple socket.io connection after reconnect Sergii Boiko 12/12/11 5:13 AM
I've did a fix, but it works only for JSONP-kind of handshake.

You can disable appropriate condition and prevents AJAX-handshake. So it will work for anybody.
Re: any workaround on multiple socket.io connection after reconnect xdanx 7/3/12 1:10 PM
Hello all,

This solution solved my problem with duplicate messages from the node.js server, after a disconnect -> connect cycle in the browser .

Before, I had all my handlers in the socket.on('connect' function() {... }); function. Moving them outside removed my duplicate messages.
What I used to do before finding this solution is to add a timestamp to every message generated by the node server and check the timestamp on the browser, and update it.
If the browser received a new message with the timestamp <= as the current browser timestamp it means that it is a duplicate message and it should reject it.

Code to solve my previous problem :

    var last_msg_time = 0;
   
    function check_duplicate(time)
    {
        console.log("Last msg time: " + last_msg_time);
        console.log("Current msg time: " + time);
   
        // IF the last msg has been received already ignore the msg
        if (last_msg_time <= time)
        {
            console.log("Msg is NOT fine!");
            return false;
        }
       
        console.log("Msg is fine!");
       
        // Message has a different timestamp
        // Update the last_msg_time and return true
        last_msg_time = time
        return true;
    }

Snippet from one of my handlers:

    socket.on('chat_client', function(data){

        var data_json = jQuery.parseJSON(data);
       
        // If this is a duplicate, then discard!
        if (! check_duplicate(data_json.time))
            return;
        var data = data_json.data;
       
        console.log("received a chat message: " + data);
    });