Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop socket.io from reconnecting

Tags:

socket.io

Simple scenario:

  1. Client connects to server with socket.io (socket = io.connect(...))
  2. Server crashes
  3. Client tries to reconnect
  4. User tells client to disconnect (socket.disconnect())
  5. Server starts
  6. Client reconnects

It seems that once socket.io starts attempting to reconnect, it cannot be stopped anymore. I want step 4 to prevent step 6 from happening, but it doesn't. What do I have to call instead?

like image 759
Lemming Avatar asked Jun 18 '12 19:06

Lemming


People also ask

Does Socket.IO reconnect automatically?

In the first case, the Socket will automatically try to reconnect, after a given delay.

How do I disable a Socket.IO server?

close([callback])​ Closes the socket.io server. The callback argument is optional and will be called when all connections are closed.

How do I know if a socket is disconnected io?

Explanation: The user logs in and after 60 seconds dropCheck is called. the dropCheck emits a ping and set a timmer of 4 seconds. the user handle the emited ping and responds with another ping.

How do I remove a Socket.IO connection?

on('connection', function(socket){ console. log('a user connected'); socket. on('disconnect', function(){ console. log('user disconnected'); }); });


1 Answers

You might want to handle the reconnection yourself.

// Disables the automatic reconnection
var socket = io.connect('http://server.com', {
    reconnection: false
});

// Reconnects on disconnection
socket.on('disconnect', function(){
    socket.connect(callback);
});

Note: old versions used reconnect instead of reconnection.

like image 133
Alexandre Kirszenberg Avatar answered Sep 20 '22 05:09

Alexandre Kirszenberg