Multiple sockets open after reconnect
I've noticed this both on my socket.io application and someone else's. When a reconnect occurs, it's common for more than one socket within a client to be open, which should never happen.
Here's a log I got from my own test application when I had a disconnect/reconnect.
*** Disconnect *** Reconnecting with delay 1000 Attempts: 1 *** Reconnecting with delay 2000 Attempts: 2 *** Connecting with xhr-polling *** Connecting with xhr-polling *** Reconnect with transport type xhr-polling Attempts: 2 *** Connected *** Connected
When this happens, I get duplicate messages sent, one for each connected socket, even though there should only be one. It is confirmed by Firebug that there are, in fact, two sockets from the client connected. My best guess is that this is in the client code, perhaps not closing the first reconnection attempt.
Of course, the real question might be why was there a disconnect in the first place?
The disconnect is fired because technically, the client has disconnected from the server. And is disconnected until the reconnect event is fired again.
But the rest is a bug.
I think this is the reason for a bug in my application as well -
On the server side, I've created a tailable cursor on mongodb to fire every time there's a new insert. This message is then sent via io.sockets.emit() to everyone connected.
But instead of receiving just one message per event, I get multiple repeat messages on the client side.
I'm on an extremely unreliable internet connection, so my guess is that this bug is affecting my code as well.
Were you guys using expressjs? I had this problem with express, rewrote it without and it worked fine.
Express would have absolutely nothing to do with this @mfkp, must have been doing something weird
Agreed with @visionmedia, its a bug in socket.io not with Express. Because the current reconnect implementation was still designed for 0.6, so it's missing some checks and clean ups.
Ah, I see. Nevermind, carry on ;-)
+1 to this. Intermittent issue in our chat app where socket.io opens multiple connections. The client then receives dupes of all emits.
+1. Websockets & chrome. Laptop goes to sleep -> app reconnects after wakup -> double events. Happens only when connecting to remote server. Cant reproduce this locally, must be some reconnect/network latency problem.
is there any workaround to get rid of multiple connection after this?, e.g from client side, if I do socket.disconnect(), will it disconnects all the connections? As I want to avoid multiple updates. does following work? 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); } })
Please, read this https://github.com/LearnBoost/socket.io/issues/474#issuecomment-2833227 . It explains why this bug occurs and how it can be reproduced.
@3rd-Eden - Any idea where this fix falls on your todo list / priorities? I've got an app that I'm building and I was planning on using this for it, and would love to be able to tell my client that it will reconnect properly from iOS devices. It disconnects after the application is put in the background or device is put to sleep.
Or do you have any idea about how I can code a workaround? I've been working on a few things for the past couple days, but best case always seems to leave too many connections open and drop all of the sockets stored data. The data isn't the end of the world I can use the reconnect trigger from the client to solve that problem, but the concurrent connections is a bit ugly.
Thanks!
@cris I'm not sure that this is the same bug that you mention. I have a very simple example of the bug without a slowdown on the server.
I can clearly reproduce this issue by:
- Implement a simple heartbeat client that sends heartbeats on 1 second intervals.
- Count the number of new connections server-side.
- Count the number of disconnections server-side.
- Start the server and make a connection with a client.
- Break the network connection between the client and server.
- Wait until the server times out and emits a disconnect event.
- Reconnect the network connection.
Observe that when the client reconnects it creates 2-4 connections emitting a 'connect' event for each. The server receives 2-4 connections from the client and emits a 'connection' event for each. The client doesn't ever close any of the incorrectly started connections.
server.js: https://gist.github.com/1717643 client.html: https://gist.github.com/1717648
Thanks. I really do think this bug should be a priority. Many commercial companies that don't know better try to hire people to develop against the socket.io framework, thinking it works well. Granted, socket.io is free, so maybe this commercial companies should start looking at commercial products. But again, I think this really needs to be prioritized as very high priority. This bug has existed since 0.7.0 and is easily reproducible.
@davidfooks, @theyak. I fixed this issue for me long time ago, and it works without a fuss.
To fix, you should:
- Apply this patch: https://github.com/LearnBoost/socket.io-client/pull/342
- Disable AJAX-handshake(and let handshake to be only via JSONP), as discribed in pull-request comment.
How do you disable AJAX handshake? I assume you can still use all the connection types, this will only affect handshake?
+1 please fix this :)
I had the same multiple connect on re-connect issue. I think this is a severe issue... The cause is that the Socket.connect method is called but the conneting flag is only set to true after handshake is completed, in case one of the matbeReconnect timers (timers handling the reconnect) wake up during the handshake process, they will calll Socket.connect again causing multiple reconnects. I have solved the issue by calling self.reconnecting at the beginning of Socket.connect method.
Created a patch: https://github.com/LearnBoost/socket.io-client/pull/424
Any chance of merging this? This bug is making a bad bug in my app as well.