question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NodeJS - socket.io-client does not reconnect when socket.io disconnects the client connection

See original GitHub issue

I’m using the following code in NodeJS and when the socket has been disconnected from the server, it will not reconnect and I cannot figure out why.

I have noticed that if the server was shut down then reconnecting will happen however if the server called “disconnect” on a client socket, then it will reconnect.

var connection, connect, onconnect, onmessage, ondisconnect, onerror;
connection = null;
connect = function(){
  var this$ = this;
  connection = require('socket.io-client')('http://MYHOST:8080', {
    reconnection: true,
    reconnectionDelay: 1000,
    reconnectionAttempts: 10
  });
  connection.on('connect', function(){
    return onconnect.apply(this$, arguments);
  });
  connection.on('reconnect', function(){
    return onconnect.apply(this$, arguments);
  });
  connection.on('disconnect', function(){
    return ondisconnect.apply(this$, arguments);
  });
  return connection.on('error', function(){
    return onerror.apply(this$, arguments);
  });
};
onconnect = function(){
  var this$ = this;
  console.log('Connection opened.');
  return connection.on('message', function(){
    return onmessage.apply(this$, arguments);
  });
};
onmessage = function(m){
  return console.log('Receiving message:', m);
};
ondisconnect = function(){
  return console.log('Connection closed.');
};
onerror = function(error){
  return console.log('Connection error:', error);
};
connect();

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
nkzawacommented, Nov 1, 2014

Does this still happen with the latest socket.io?

1reaction
almosnowcommented, Dec 4, 2021

Is there a way to discern between the two scenarios?

Disconnect due to explicit disconnect from the server vs. all other reasons?

Edit: I just found out that the disconnect event sends a ‘reason’ argument with this info. Leaving this here in case someone comes here in the future.

Read more comments on GitHub >

github_iconTop Results From Across the Web

socket.io client can't connect/reconnect reliably - Stack Overflow
I am utilizing socket.io V3.1.1 and am trying to figure out how to get the client to reconnect after the connection is disconnected...
Read more >
Troubleshooting connection issues | Socket.IO
IO client will always try to reconnect, unless specifically told otherwise. Let's review how you can troubleshoot a connection failure.
Read more >
The Socket instance (client-side)
The Socket instance emits three special events: connect; connect_error; disconnect. Please note that since Socket.IO v3, the Socket instance ...
Read more >
Troubleshooting connection issues | Socket.IO
Problem: the socket gets disconnected​​​ First and foremost, please note that disconnections are common and expected, even on a stable Internet  ...
Read more >
The Socket instance (client-side)
connect ; connect_error; disconnect. Please note that since Socket.IO v3, the Socket instance does not emit any event related to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found