Srini Kandula
Srini Kandula

Reputation: 1001

socket.io javascript client fails to reconnect after restarting server

I'm using "socket.io": "1.4.5","socket.io-redis": "1.0.0". The javascript client using socket-io-client seems to be happily connecting to the server except for when the server is restarted. The connection attempt fails triggering 'connect_error' event with message '{"message":"xhr poll error", "type":"TransportError","description":0"}'. If I restart by browser the connection works just fine. I also noticed that if I set a breakpoint in event handler for 'connect_error' and wait for a few seconds it the connection attempt succeeds eventually. I'm missing something a config setting in client code? here is the client code I have

var socketConnectParams = {'max reconnection attempts': 150};  
    socketConnectParams['reconnection'] = true;
    socketConnectParams['reconnectionDelay'] = 5000;
    socketConnectParams['reconnectionDelayMax'] = 15000;
    var socket = io('http://localhost:8888', socketConnectParams);

    socket.on('connect', function () { });

    socket.on('connect_error', function(error) {
      dLog("connection error event received." + JSON.stringify(error));
    });

Upvotes: 1

Views: 2874

Answers (1)

noderman
noderman

Reputation: 1944

I listen to a "disconnect" event to rebind a new 'connect' event. This way, if you server is restarted, the local client will detect this and create a new listener for then the server comes up again. Maybe you could try that.

        socket.on('disconnect', function(){
            socketCleanup(); // this is a function to cleanup all listeners, just in case, so you can restart fresh
            socket.on('connect', function(){
                socketConnect();
            });
        });

Upvotes: 5

Related Questions