Skip to content

Connect -> Disconnect -> Connect doesn't work #251

Closed

Description

The following code doesn't work for me:
socket = io.connect('http://host.com:8081');socket.disconnect();socket = io.connect('http://host.com:8081');

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

Activity

  1. aarong commented on Jul 27, 2011

    @aarong

    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 () {
    
        console.log('Connected');
    
        if (numDisconnects > 0) {
            console.log('Disconnecting');
            ns.socket.disconnect();
    
            setTimeout(function () {
                console.log('Reconnecting');
                ns.socket.connect();
            }, 5000);
        }
        numDisconnects--;
    
    
    });
    
    ns.on('disconnect', function () {
        console.log('Disconnected');
    });
    
  2. Gorokhov commented on Aug 18, 2011

    @Gorokhov

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

  3. milani commented on Aug 29, 2011

    @milani

    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!

  4. skrat commented on Sep 21, 2011

    @skrat

    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.

  5. respectTheCode commented on Oct 3, 2011

    @respectTheCode

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

  6. milani commented on Oct 4, 2011

    @milani

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

  7. skrat commented on Oct 4, 2011

    @skrat

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

  8. mrjoes commented on Nov 1, 2011

    @mrjoes

    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.

  9. milani commented on Nov 1, 2011

    @milani

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

  10. skrat commented on Nov 1, 2011

    @skrat

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

  11. milani commented on Nov 1, 2011

    @milani

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

  12. skrat commented on Nov 1, 2011

    @skrat

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

  13. mrjoes commented on Nov 1, 2011

    @mrjoes

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

    Not very good idea to be honest.

  14. milani commented on Nov 1, 2011

    @milani

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

  15. mrjoes commented on Nov 1, 2011

    @mrjoes

    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.

  16. 4 remaining items

  17. cacois commented on Nov 28, 2011

    @cacois

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

  18. baradas commented on Feb 8, 2012

    @baradas

    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.io@0.8.7

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

  19. zanhsieh commented on Mar 19, 2012

    @zanhsieh

    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:
    io.sockets[base].disconnect();

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

    note: socket.io version - 0.9.0, platform: windows 7 home premium

  20. rawberg commented on Oct 15, 2012

    @rawberg

    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

  21. PEM-- commented on Dec 27, 2012

    @PEM--

    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

  22. lin04com commented on Feb 19, 2013

    @lin04com

    It works for me! Thanks @mrjoes.

  23. Plasma commented on Jun 25, 2013

    @Plasma

    Thanks @milani

  24. danjunger commented on Jun 6, 2014

    @danjunger

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

  25. rauchg commented on Jun 6, 2014

    @rauchg
    Contributor

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

  26. pitambar commented on Jul 17, 2014

    @pitambar

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

  27. peteruithoven commented on Aug 14, 2014

    @peteruithoven

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

  28. maxcodefranco commented on Aug 19, 2014

    @maxcodefranco

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

  29. rolandhegedus commented on Mar 14, 2015

    @rolandhegedus
    Client.prototype.connect = function() {
        if (!this.connection) {
            this.connection = this.socket.connect(config.host + ':' + config.port);
        }
        else {
            if (!this.connection.connected) {
                this.connection.connect();
            }
        }
    };
  30. nasasutisna commented on Mar 1, 2019

    @nasasutisna

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

    this solve my problem, thank you mr. milami

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions