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.

The right way for a client to reconnect after the server does a socket.disconnect( true )

See original GitHub issue

Server: node + socket.io

var io = require( 'socket.io' )( 3000 );

io.on( 'connection', function ( socket ) ...

// at some point if client auth fails, server may kick him out:

socket.disconnect( true );

Client: html/js + socket.io 1.4.5

socket = io.connect( 'http://127.0.0.1:3000', {
    reconnection: true,
    reconnectionDelay: 1000,
    reconnectionDelayMax : 5000,
    reconnectionAttempts: 99999
} );

socket.on( 'connect', function () {
    console.log( 'connected to server' );
} );

socket.on( 'disconnect', function () {
    console.log( 'disconnected to server' );
} );

The above is simplified but that is the basis. Reconnection works fine if I fire up the client when the server is down. The client tries to connects to the server and when I finally fire up the server the connection is estabilished.

At some point it may happen that the server decides to disconnect the client, mainly because he logged out invalidating the token and a query on the db tells to do so, or the auth token expired or whatever.

IN this circumstance I’d like the restore the server polling by the client because the user may want to login again and he has the rights to connect again to the server.

If I refresh the page (client) in the browser it works okay but it’s an ugly solution.

I think the cause is the “Manager.skipReconnect” property set to true after doing disconnect(), so I’m asking what is the proper way to reinitiate a reconnection polling by the client after the server disconnects a client.

Found some answers on stackoverflow about this but all were unsuccessfull, all stuff to be executed in the client:

  • socket.io.reconnect();
  • socket.socket.reconnect(); // ??
  • socket = io.connect( ‘http://127.0.0.1:3000’, … // reinvoking the connect snippet seems bad idea

Could you help me about this matter?

Thank you

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:27
  • Comments:35 (1 by maintainers)

github_iconTop GitHub Comments

52reactions
DrLightmancommented, Mar 9, 2016

For now, I managed this way and it seems to be stable:

Client:

var app = {
...
socket: null,
connect: function() {
  var self = this;
  if( self.socket ) {
    self.socket.destroy();
    delete self.socket;
    self.socket = null;
  }
  this.socket = io.connect( 'http://127.0.0.1:3000', {
    reconnection: true,
    reconnectionDelay: 1000,
    reconnectionDelayMax : 5000,
    reconnectionAttempts: Infinity
  } );
  this.socket.on( 'connect', function () {
    console.log( 'connected to server' );
  } );
  this.socket.on( 'disconnect', function () {
    console.log( 'disconnected from server' );
    window.setTimeout( 'app.connect()', 5000 );
  } );
}
...
} // var app
11reactions
pankajtalaviya97commented, Jul 27, 2018

Use 0.8.3 version of io.socket remove new version 1.0.0

compile('io.socket:socket.io-client:0.8.3') {
    exclude group: 'org.json', module: 'json'
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

The Socket instance (client-side)
This event is fired upon disconnection. ... In the first two cases (explicit disconnection), the client will not try to reconnect and you...
Read more >
How to reconnect after you called .disconnect() - Stack Overflow
The standard approach in latest socket.io is: socket.on('disconnect', function() { socket.socket.reconnect(); }).
Read more >
C# Remote Access Tool - Problem with socket reconnection
The only way for the server to get disconnected is to close the program or kill the connections (It can't crash at an...
Read more >
Socket Programming in Python (Guide)
The client calls .connect() to establish a connection to the server and initiate the three-way handshake. The handshake step is important ...
Read more >
A Guide to Java Sockets - Baeldung
Our current server blocks until a client connects to it, and then blocks again to listen to a message from the client. After...
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