Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.io Knowing when max reconnection attempts limit has been reached

I have a node socket server and a client connection.

If the socket server goes down for a short period, the client is able to re-establish a connection without me doing anything. However if the client goes down for a long time, the delay between trying to reconnection attempts from the client is too long (the delays between re-connection attempts get longer with each failed reconnection).

To get around this, I want to set 'max reconnection attempts' to a small number, and if they have been exhausted, I want to run my own polling code.

My question is, how can I know when when the max reconnection attempts limit has been reached on the client side?

socket.on('disconnect') is called even while the socket is trying to re-establish the connection, and may in fact succeed, so is the incorrect place to attempt to call my other code.

Here is a small snipet from my client code:

function start_socket(){
        console.log("socket trying to connect");
        socket = io.connect('http://someip/', { 'force new connection': true , 'max reconnection attempts' : 0});
        console.log("...here");
        socket.on('connect', function() {
                console.log('connected to server');
                set_loggedin_status('true');
        });
        socket.on('something', function(link) {
          console.log("something: " + something);
          openTabWithURL(something);
        });
        socket.on('disconnect', function() {
                console.log('disconnected from server');
                set_loggedin_status('false');
                close_socket();
        });
        socket.on('error',function(){
                console.log('socket error');
        });
}

function do_my_own_polling(){
  //some code here.
}
like image 933
Simon Avatar asked Sep 11 '25 23:09

Simon


1 Answers

I think you're trying to achieve the result in a wrong way: if I understood right you need to keep looking for the server forever with a fixed poll-delay.

Instead of starting your own polling mechanism when the maximum number of reconnection attempts occur (which requires additional "meta" source code) you can configure socket.io to do so. Particularly, you could try to use the following options (from https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO):

  • reconnection limit: allows you to set a maximum time delay between each poll/reconnection attempt. For instance, setting this to 3 seconds, socket.io will make the first poll after 500ms (which is the default value of reconnection delay), the second one after 1s, the third after 2s, but the fourth will be after 3s and not 4. The following attempts will have a distance of 3s in time;
  • max reconnection attempts: this sets the number of reconnection attempts that socket.io will make: if you want socket.io to try forever, set it to "Infinity".

So, the following will work:

socket = io.connect('http://someip/', {'reconnection limit' : 1000, 'max reconnection attempts' : 'Infinity'});
like image 190
reallynice Avatar answered Sep 13 '25 12:09

reallynice