socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

reconnect_failed gets never fired

Open Pita opened this issue 15 years ago • 27 comments

If I stop the node server so every reconnect of the client fails, the reconnect_failed event gets never fired

Here is the position in the code where reconnect_failed should get fired https://github.com/LearnBoost/socket.io-client/blob/master/lib/socket.js#L501

for some reason it never enters the else part

Pita avatar Nov 19 '11 21:11 Pita

I am having the same issue. It is reproducible with a simple test case:

app.js:

var io = require('socket.io').listen(4000);

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

app.html:

<html>
<head>
<script src="http://localhost:4000/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:4000',
    {'reconnection delay': 100,
     'max reconnection attempts': 4});
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });

  socket.on('connect', function () {
    console.log("connect");
  });
  socket.on('disconnect', function () {
    console.log("disconnect");
  });
  socket.on('connecting', function (x) {
    console.log("connecting", x);
  });
  socket.on('connect_failed', function () {
    console.log("connect_failed");
  });
  socket.on('close', function () {
    console.log("close");
  });
  socket.on('reconnect', function (a, b) {
    console.log("reconnect", a, b);
  });
  socket.on('reconnecting', function (a, b) {
    console.log("reconnecting", a, b);
  });
  socket.on('reconnect_failed', function () {
    console.log("reconnect_failed");
  });

</script>
</head>
<body>
hello world
</body>
</html>

Run node on the app.js file. Open the HTML file in a browser. Open the debug console in the browser. Note that you got a 'connected' event. Control-c the node server. Note that you get a bunch of reconnecting messages, but never a reconnection_failed.

The reconnection options don't seem to make a difference. I included them here so you don't have to wait a long time to see the failure.

n1mmy avatar Dec 14 '11 01:12 n1mmy

+1

rkirks avatar Jan 30 '12 14:01 rkirks

the same

Selvatico avatar Feb 16 '12 09:02 Selvatico

+1, here's how I'm dealing with it:

Client side code (Coffeescript):

# Set max reconnects here
max_reconnects = 5

socket = io.connect("http://localhost:3000",
  "reconnection delay": 100
  "max reconnection attempts": max_reconnects
)

socket.on "reconnecting", (delay, attempt) ->
    console.log "attempting reconnect - " + attempt
    if attempt is max_reconnects
        console.log "all reconnect attempts failed"
        # Show the user an error etc.

This will return the following in the console.log after you Ctrl+C the server:

  1. attempting reconnect - 1
  2. attempting reconnect - 2
  3. attempting reconnect - 3
  4. attempting reconnect - 4
  5. attempting reconnect - 5
  6. all reconnect attempts failed

dhruv-bhatia avatar Apr 10 '12 10:04 dhruv-bhatia

Same, the issue is still here.

maxailloud avatar Apr 12 '12 10:04 maxailloud

Same here, testing on 0.9.5. The problem happens in the following block: https://github.com/LearnBoost/socket.io-client/blob/master/lib/socket.js#L520

Plus, redoTransports is always undefined on the first time. Seems this is not considering when try multiple transports has been disabled, which should make the the reconnect_failed to always fire in those cases as well.

edwardbc avatar Apr 12 '12 18:04 edwardbc

Same issue here.

rakyll avatar Apr 17 '12 08:04 rakyll

I ran into this problem as well. I've definitely found the cause, hopefully a fix. redoTransports is undefined at first because socket.io only tries multiple transports on the last reconnection attempt (this is a side note in the config doc). I think the intention here is to do one last connection attempt with all transports, and finally give up.

The problem is, the handshake is always done with XHR or jsonp. If the server is not reachable, the handshake will always fail. In the handshake code, there is a line: if (xhr.status == 200) { complete(xhr.responseText); } else { !self.reconnecting && self.onError(xhr.responseText); } } this bizzare, hard to read line !self.reconnecting && self.onError(xhr.responseText); means that self.onError will only be called if self.reconnecting is false. (I don't like this kind of shorthand - use an else if). Normally, there is a reconnect timer that will fire, triggering another call to reconnect. However, the timer is not set up on the last reconnection attempt. This code path never runs, meaning reconnect_failed is never called.

The solution I think is to also start the timer on the final reconnection attempt. Once I figure out how, I'll submit a pull request for a patch.

nynexman4464 avatar Apr 25 '12 16:04 nynexman4464

+1 I can't get connect_failed or reconnect_failed to emit

eephillip avatar May 23 '12 21:05 eephillip

I managed to fix the problem by changing this section:

if (!self.redoTransports) {
  self.on('connect_failed', maybeReconnect);
  self.options['try multiple transports'] = true;
  self.transport = self.getTransport();
  self.redoTransports = true;
  self.connect();
} else {
  self.publish('reconnect_failed');
  reset();
}

After the self.connect() line I added:

self.publish('reconnecting', self.reconnectionDelay, self.reconnectionAttempts);
self.reconnectionTimer = setTimeout(maybeReconnect, self.reconnectionDelay);

This causes the event to get fired properly after the last failed attempt.

robbrit avatar Jun 11 '12 17:06 robbrit

Nice, the connection failure event is trigging for me with that mod.

eephillip avatar Jun 12 '12 02:06 eephillip

@robbrit i just found that the changes what you mentioned are already there in my socket.io.js but still the reconnect_failed event is not getting fired. And also one more thing what i found it, even if i specify 10 reconnect attempts it tries only 5 or 6 times and stops. Please any one help to solve this.

veeru-as avatar Jun 15 '12 05:06 veeru-as

@veeru-as The first code block is what is already there, the second code block is what I added. Also keep in mind that the reconnect delays are exponential, so after a while it seems like it has stopped but it is just waiting a long time.

robbrit avatar Jun 15 '12 14:06 robbrit

Is there any partical reason why this reconnect_failed is in the docs but it never triggers?

jrmyio avatar Sep 06 '12 20:09 jrmyio

Is there any reason why this bug has not been fixed??

jsilveira avatar Nov 14 '12 04:11 jsilveira

Because you didn't make a pull request for it yet?

On 14 nov. 2012, at 05:31, jsilveira [email protected] wrote:

Is there any reason why this bug has not been fixed??

— Reply to this email directly or view it on GitHub.

3rd-Eden avatar Nov 14 '12 06:11 3rd-Eden

I went through it in chrome. The variable causing the issue is "self.redoTransports" because it is always undefined. This is most likely a breakdown in the config section of the code.

haddow777 avatar Dec 15 '12 00:12 haddow777

Same.

dizballanze avatar Dec 17 '12 16:12 dizballanze

The fix from robbrit still appears to work for me in 0.9.11.

eephillip avatar Jan 17 '13 23:01 eephillip

Can we please get this pushed in? ffs.

matbeedotcom avatar Feb 14 '13 19:02 matbeedotcom