Łukasz
Łukasz

Reputation: 13

socket.io network connectivity failure detection

I am facing problem with ERR connection Timed Out using socket.io whenever user lost access to network. The thing is that when user is losing connection with the network the server does not notice it and it seems like user is still able to trigger some events. How to properly disconnect both: -client from the server from client perspective on network failure -client from the server from server perspective on network failure

So basically I have hosted an app here: askit-break-scheduler.herokuapp.com, if I then connect with app and suspend my connection, for example by putting my laptop into sleep mode, some data is lost and my nickname seems to be set to undefined. Can you please help me to figure out what am I missing here? Code can be found here: github.com/Hoopoe99PL/break-tracker

I manage http connection as follows in node:

    app.get("/", (req,res)=>{
    fs.readFile(__dirname + '/src/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
});
app.use(express.static(__dirname + "/src"));

and then manage the IO connection and disconnect event:

io.on("connection", (socket)=>{


 //some other code here


    socket.on('disconnect', ()=>{

// deleting all sessions data, clearing all entries in arrays and Maps regarding that socket

        cancelUserStatus(socket.id);
        users.delete(socket.id);
        io.emit('u-users-list', Array.from(users));
        clearInterval(intervals.get(socket.id));
        intervals.delete(socket.id);
    });

});

my problem is that whenever user suspends network connection and then tries to reconnect again it seems like some data is missing because for example the username is set to undefined.

I have view server's logs and there seems not to be any output about new connection as I print new connection's socket id into the console earlier in the code as well.

I am thinking about somehow forcing user to reconnect again to the server because it all seems like when user lost network connection and then gets it again he is instantly connected to the app but the verification process being launched at the start of the app when entering url into web browser seems not to be triggered here.

Any client side handlers would be needed I guess.

EDIT: The client side handlers provided in comments below might be an answer, will test later on.

Upvotes: 1

Views: 1720

Answers (1)

jfriend00
jfriend00

Reputation: 707158

If you have clients that have connection issues, then socket.io will already be trying to detect those connection issues and reconnect. About the best you can do on top of that is to show feedback in the UI that there are connection issues and, once you lose the connection perhaps you even disable the controls or put up an error when they click on a control to inform that user that you've lost your connection and can't carry out the operation.

The relevant client-side events that you can listen to are:

error
connect_error
connect_timeout
reconnect
reconnecting
reconnect_error
reconnect_failed

You can read in the client-side doc what exactly each of these events mean. Some of these events mean you've lost the connection and some of these events are steps along the way to trying to re-establish a connection.

Upvotes: 0

Related Questions