Connect -> Disconnect -> Connect doesn't work

Created on 23 Jul 2011  ·  33Comments  ·  Source: socketio/

The following code doesn't work for me:
socket = io.connect('');socket.disconnect();socket = io.connect('');

This is a workaround I've found:
socket = io.connect('');socket.disconnect();delete io.sockets[''];io.j = [];socket = io.connect('');

Unable to reproduce bug

Most helpful comment

Use this one: io.connect(null,{'force new connection':true});

All 33 comments

Same issue here -- was unable to connect-disconnect-reconnect the way you described.

However, I've found that the following works. Oddly, if you omit the setTimeout and reconnect immediately (or after too short a timeout) then you are immediately disconnected.

var ns = io.connect(
        { reconnect: false }
    numDisconnects = 2;

ns.on('connect', function () {


    if (numDisconnects > 0) {

        setTimeout(function () {
        }, 5000);


ns.on('disconnect', function () {

socket = io.connect(host);
socket.socket.reconnect(); || socket.socket.connect(host);
works, but since reconnect or second connect disconnect is fired every couple of seconds

I had the same issue so I could not finish my node knockout entry in time due to this bug :(
Please fix it before any other heart break!

None of the above workarounds worked for me. Guys, this is serious show stopper. Why the "Unable to reproduce" tag? My testing env. is latest Chrome on OS X 10.6.

I am seeing the same thing. Also calling io.connect() a second time fails without an error. Has anyone found a workaround?

Use this one: io.connect(null,{'force new connection':true});

How convenient, and ugly. What about removing io.sockets[uuri] = socket; from io.js:196? What's the point of that punch line?

Unfortunately, same problem here. You can not reconnect after disconnection.

Only thing that worked for me after disconnection:
io.j = [];
io.sockets = [];

Edit: it is not only one problem of the client-side disconnect(). It does not clean up data structures on disconnect - you can see that SocketNamespace.$events still has all callbacks for closed connections, etc.

@MrJoes Use io.connect(null,{'force new connection':true});

All right, is this really the final API for reconnecting? Rather strange.

@skrat I agree with you. there should be an io.reconnect API.

I don't think so, io.connect then io.disconnect then io.connect should just work, as expected.

@milani: and leak memory due to orphaned sockets, event handlers, etc staying in io.sockets[]?

Not very good idea to be honest.

@MrJoes Are you sure about memory leak? @3rd-Eden from io.socket told me to use this API.

Nevermind, it won't add socket to io.sockets[] if you pass 'force new connection'.

Though, it is a bit ugly, especially with endpoints, because you're losing multiplexing - it relies on socket in io.sockets to do multiplexing.

@Gorokhov suggestion of using reconnect through the socket prototype worked well for me:

Same issue here - socket.reconnect() isn't working for me, I'll try a few other workarounds. This is disappointing though, its a pretty basic piece of functionality to break in an update. I"m sure a lot of applications are unable to easily update because of this.

If devs need any assistance reproducing the issue, I'm happy to help - i'm surprised to see the "unable to reproduce" tag on this bug.

It appears like there will be a major update soon because of this pull request:
@cacois Try using socket.socket.reconnect() rather than socket.reconnect()

ah, great news :-)

@wackfordjf3 Hey thanks, good call. Works now. :)

Still facing issues with reconnect.
2nd reconnect causes multiple reconnection events to be fired on the server. (and this multiplies with every subsequent reconnect). Am firing reconnects on a namespace.

socket = io.connect(url{'reconnect':false});socket.disconnect;// 1st disconnect
socket.socket.reconnect();socket.disconnect(); //2nd disconnect
socket.socket.reconnect(); // 2nd reconnect

Am on : socket.[email protected]

Further to this :
Ugly way works fine :
delete io.sockets[url];io.j = [];
socket = io.connect(url);

Face the same thing as people already have here; even weird in namespace scenario. Drop the solution for namespacing:

var base = 'http://localhost:3000'
, channel = '/chat'
, url = base + channel;

var socket = io.connect(url);

// call socket.disconnect() won't disconnect, so:

// reconnect syntax.
delete io.sockets[base]; io.j =[];
socket = io.connect(url);

note: version - 0.9.0, platform: windows 7 home premium

spent an afternoon wrestling with unit tests that were timing out because I couldn't connect after disconnecting. {'force new connection':true} resolved the issue. thanks @milani

Same issue here using Cucumber.js. As I have to create and destroy objects, I was having facing the same connect, disconnect, connect failed. Thx @milani and @rawberg

It works for me! Thanks @mrjoes.

Thanks @milani

The 'force new connection' setting is now called 'forceNew' in the v1.0 release for those that might have run into this issue again.

I pushed a commit to add BC with that, so soon no one should have this issue anymore!

it worked 4 me, socket = io.connect(socketUrl,{'forceNew': true });

forceNew disables the whole namespace multiplexing system, is that really the solution?

I'm using:
socket= io.connect()

Client.prototype.connect = function() {
    if (!this.connection) {
        this.connection = this.socket.connect( + ':' + config.port);
    else {
        if (!this.connection.connected) {

io.connect(null,{'force new connection':true});

this solve my problem, thank you mr. milami

Was this page helpful?
0 / 5 - 0 ratings