NodeJS - socket.io-client does not reconnect when socket.io disconnects the client connection
See original GitHub issueI’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:
- Created 9 years ago
- Comments:7 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Does this still happen with the latest socket.io?
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.