How configure reconnecting in socket.io?

I create socket.io connection with next code

var socket = new io.connect('http://localhost:8181', {
    'reconnect': true,
    'reconnection delay': 500,
    'max reconnection attempts': 50
});

But when I kill server CTRL+C and start it again, reconnection isn't happening. But disconnect event raised on client side. What maybe reason of it?

Answers


This is an old question, but for other people like me, who are looking how to configure reconnect in socket.io (1.x) here is a correct syntax:

var socket = new io.connect('http://localhost:8181', {
    'reconnection': true,
    'reconnectionDelay': 1000,
    'reconnectionDelayMax' : 5000,
    'reconnectionAttempts': 5
});

I realise this is an old question, but I've been having some trouble with socket io reconnecting and found this post high in the search results, so thought I would contribue. Try debugging exactly which events are firing using the following code:

# coffeescript. compile if you're writing javascript, obviously.

socket.on 'connect',-> console.log 'connected'
socket.on 'reconnect',-> console.log 'reconnect'
socket.on 'connecting',-> console.log 'connecting'
socket.on 'reconnecting',-> console.log 'reconnecting'
socket.on 'connect_failed',-> console.log 'connect failed'
socket.on 'reconnect_failed',-> console.log 'reconnect failed'
socket.on 'close',-> console.log 'close'
socket.on 'disconnect',-> console.log 'disconnect'

This should give you more insight into the state of the client socket.

Also, try looking in the Network tab of your web inspector to see if it is firing XHR requests as a fallback. Finally, in your web console, try typing io.sockets and expand it out to see whether it is actually trying to reconnect or not.

I have encountered problems with reconnect_failed not firing, and the reconnect tally not resetting. The following are links to discussions of these issues on github.

reconnection delay - exponential back off not resetting properly

reconnect_failed gets never fired

some potential fixes/workarounds


This is an old question, but I had the same question (for a different reason) when using v1.4.5. My chat room app worked beautifully, but when I hit Ctrl+C in the terminal, my browser continued to loop and report ERR_CONNECTION_REFUSED every few seconds until I shut it down.

Changing a previous answer just a bit gave me the solution.

For v1.4.5, here is my original code for "var socket" in my client js file:

    var socket = io();

And here is the solution:

    var socket = io({
        'reconnection': true,
        'reconnectionDelay': 1000,
        'reconnectionDelayMax' : 5000,
        'reconnectionAttempts': 5
    });

Obviously I could change the values if I want, but the important point is that this killed the never ending reconnection requests.


reconnection delay is too small 500ms increase that, on top of that 50 retries means 500 * 50 = 25000 ms which is 25 seconds. If that doesn't help set a timeout on error event on client side to recreate the socket object (After error and some delay retry to create connection).


Need Your Help

Smoothing a jagged path

java image image-processing java-2d edge-detection

I was participating in the thread Image/Graphic into a Shape the other day and made a hackish attempt to get the outline of an image by adding a Rectangle iteratively to an Area. That was very slo...

Open Powershell in a specific directory from shortcut

powershell windows-7 shortcuts

This sounds like it should be so simple... I must be dumb.