Double connect
somehow double connect happens to clients(meaning all the handlers are called twice), could not reproduce the situation on my own, but keep receiving bug reports, the behavior is exactly the same if you call socket.socket.connect() on already connected socket.(tested on websocket and flashsocket)
as quick hack you can use "force new connection": false configuration
I also observed this bug in our application. Reproduce was next. In FF with websocket connection, some error happened. Then one after other 3 reconnecting events had generated. After this 3 new connections was established, and browser received 3 messages instead of one.
Maybe some bug in reconnection logic? Something, which generates several reconnections instead of one.
I've seen issues in older browsers that don't support websockets (e.g., FF 3.6) where initializing socket.io in the dom ready event ("$()" in jQuery) causes multiple connections whereas initializing in the later window.load event does not. I've found this particular issue to be consistently reproducible. Not sure if it's the same thing you're seeing but the symptoms look very similar.
Same issue here as well. Problem is if the server crashes for whatever reason and then is powered back on, the reconnect results in n+1 responses every single time. so if we have say 3 server crashes, we have 4 responses coming in on each server emit. Refreshing the page resolves the issue. Has anyone found a solution for this [even if it is temporary?]
This may not be a permanent solution, but i rearranged my code so that each event is bound independently and the duplication issue seems to have been resolved on the auto reconnect
me.socket.on('connect', function () {
});
me.socket.on('message', function(data) {
});
me.socket.on('disconnect', function() {
});
After a week of debugging via catched Wireshark dump, I've managed to find the reason and reproduce for this bug.
In short, reconnection logic is very fragile. It depends on many timers which can run in parallel and it leads to several reconnects. This line https://github.com/LearnBoost/socket.io-client/blob/master/lib/socket.js#L511 is a main reason for this bug.
Now full reproduce:
- Connect with client to server.
- Put slow code into your server (we should suspend node.js mainloop for 4-8 seconds) via factorial: function fibo(n) { if (n < 2) return 1; return fibo(n-2) + fibo(n-1); } Put it into auth-section. This should be called on handshake: io.set('authorization', function(data, accept) { console.info("Fibo :: " + fibo(41)); You should experiment in your node console, to find fibo(N), which will block main-loop for 4-8 seconds.
- Now, you should do fast restart of your server. Slow code will apply for connection. Client should be notified it is not handshaked. And now it tries to reconnect. In console you will see several reconnect attemps(if you put logging on "reconnect/reconnecting" callbacks). After our slowdown hack it will be repeated forever.
In real life this reproduced via slowdown of server-reply at least for several seconds. This can be when node.js is under heavy-load and responds with some delay. Or some network slowdown can also lead to such behavior. It can be also reproduced after server restart.
This line(socket.js, 511): self.reconnectionTimer = setTimeout(maybeReconnect, self.reconnectionDelay);
schedules event after connect call. And if connect-response is delayed for at least one second, it is triggered and put new reconnect into queue. After 2 seconds it adds another one and so one.
I did a fix, but it isn't tested and looks not very solid. Main issue - how to reason about which callback/timer can be run concurrently to maybeReconnect function. This line: https://github.com/LearnBoost/socket.io-client/blob/master/lib/socket.js#L490 can also lead to duplicated connection.
Socket.io client can be much more simple to reason about, if it will be refactored to use some State Machine. Currently state is spreaded across different flags(connected, connecting, reconnecting, etc). And in many functions I saw guards like "if (!self.reconnecting)" and many others. State machine can simplify this logic to have set of states and events. And timers can be used only to fire event. If this event hit incorrect state, state-machine can just ignore this event and don't bother itself.
I haven't found good STM for JS, but this one from Ruby can be easily ported to JS: https://github.com/geekq/workflow
Definitely +1 on getting this issue repaired. I've been working on trying to find a work around for this problem without modifying socket.io or socket.io-client directly, but unfortunately the only method that I've reliably come up with is just disabling reconnect. Which is definitely not a good solution, especially with the increased usage of mobile devices, reconnecting is a huge requirement.
Does anyone have any idea how this falls on the developers priority list?
Ah. Looks like this issue has been assigned to 3rd-Eden from issue: https://github.com/LearnBoost/socket.io/issues/430
I've done a fix for a client and sent pull request. It works good in 0.8.4 and can work good in other versions. But it requires to disable in sources ability to use AJAX(or CORS) for handshake. See this: https://github.com/LearnBoost/socket.io-client/pull/342 for details.
Sorry for reviving such an old issue. I'm using I believe the latest version of socket.io (or at least, I did npm install socket.io). This issue still occurs for me, I'm relatively new to socket.io and node.js as a whole. I've also had issues where sometimes the first connection (out of the two that I've only had happen at any one time) has a read error. If I'm correct in saying cris, your fix was committed so it should not happen anymore but it still does unless I missed an important factor?
Edit -
It appears cris forked socket.io and made a fix on that and the fix was not committed to the original socket.io?
@JTallis I had the same issue as @gdiz and his suggestion worked well. If your issue is similar, I suggest you try it and let us know how it works.
I'm also having this issue with 0.9.16 which I believe is the most current version. How is gdiz work around preventing this from occurring? Didn't completely understand it.
omg... i spend several hours to figure out what's wrong with my app, and why messages between server and client mass duplicated after connection lost and renew...
using 0.9.16 i can confirm this issue
I can get a double connection event at will with the following client and server code in the same file node.js file:
"use strict";
var server = require('socket.io');
var client = require('socket.io-client');
setTimeout(function () {
var io = server.listen(8888);
io.of('/chat').on('connection', function (socket) {
console.log('Server: /chat connection');
});
io.sockets.on('connection', function (socket) {
console.log('Server: connection');
});
}, 2000);
var socketAddress = 'http://localhost:8888/chat';
var socket = client.connect(socketAddress);
socket.on('connect', function () {
console.log("Client: connect");
});
socket.on('error', function () {
console.log("Client: error");
socket.socket.reconnect();
});
A couple of odd things:
-
If I change the namespaced connection to a normal one by removing the "/chat" from the url there is then only one connection event.
-
If I start the server immediately, change the setInterval time to zero, there is no initial connection error and only one connection event.
Any work arounds or fixes for this?
Having the same issue. If the socket.io server restarts unexpectedly the client reconnects twice so every message is then processed twice by the client. Really messes up my counts on the client end.
I have same problem with 1.0.0-pre2 release. I have two "400 Bad Request" when i restard socket.io.
Will look into this today!
Will look into this today!
Do not hesitate if you need more details, logs or screens! It's not every times.
in client socket.io.js on 1.0.6 on line 2755:
Request.prototype.create = function(isBinary, supportsBinary){ var xhr = this.xhr = new XMLHttpRequest({ agent: this.agent, xdomain: this.xd }); ... }
I believe it is a good idea to set : xhr.timeout = instance of Manager._timeout - 10 ms This way you prevent multiple client socket creation on the client side. On the server side multiple sockets will heartbeat timeout.