0

Socket.io how to handle client internet disconnect event? I read heartbeat on stackoverflow but that solution does not work for me.

I found a ping solution but I think it is a bad approach to ping all the connected user, it will slow down the server

here is my code, kindly guide how to handle internet disconnect event?

io.on('connection', function(client) {
    client.on('online', function(data) {
        socket_api.user_online(data.user_id, function (err,res){
            if (err) {
                client.emit ('error',err);
            } else {
                console.log ("client connected")
                client.emit ('online_response', res);
            }

        });
        // client.emit('messages', 'Hello from server');
    });
    client.on('disconnect', function (data){
        console.log ("Client disconnected");
    });
});
Community
  • 1
  • 1
  • `ping-pong` based solution is required for `websocket` – Mukesh Sharma Jan 04 '17 at 08:29
  • When the client loses its internet connection, the server will get a disconnect event. It might not be immediate, but it will happen. I think your question is probably a dup of this: [how to detect that user's connection is lost or he closed the browser window in Nodejs socket.io](http://stackoverflow.com/questions/16293520/how-to-detect-that-users-connection-is-lost-or-he-closed-the-browser-window-in) and this one [Socket IO detect when client has lost connection](http://stackoverflow.com/questions/9182695/socket-io-detect-when-client-has-lost-connection). – jfriend00 Jan 04 '17 at 08:33
  • Please guide me the ping solution will not effect the node server performance? Is it a good approach ? – سافٹ ویئر انجینئر Jan 04 '17 at 08:34
  • socket.io already does ping and pong and will detect a lost connection in its default configuration - you do not have to add more ping/pong. There are socket.io configuration parameters that control how often ping and pong are set and how long the socket.io server code waits before declaring a lost connection. – jfriend00 Jan 04 '17 at 08:35
  • if it detects connection lost after some time, then what is the real time left in it ? It will be no more real time then – سافٹ ویئر انجینئر Jan 04 '17 at 08:43
  • @jfriend000 after connection lost it marks user disconnect after 2mins – سافٹ ویئر انجینئر Jan 04 '17 at 08:48
  • Can you please tell me how to reduce the time for marking user disconnect ? What exactly socket is doing is, when user lost connection it marks the user disconnect after 2mins, I want to reduce this delay to 1min – سافٹ ویئر انجینئر Jan 04 '17 at 08:55

1 Answers1

1
 // use this on server side
function sendHeartbeat(){
    setTimeout(sendHeartbeat, 8000);
    io.sockets.emit('ping', { beat : 1 });
}

io.sockets.on('connection', function (socket) {
    socket.on('pong', function(data){
        console.log("Pong received from client");
    });
}

setTimeout(sendHeartbeat, 8000);

.................................................

// on Client side 
socket.on('ping', function(data){
          socket.emit('pong', {beat: 1});
        });

hope this help you

Manjeet Thakur
  • 2,288
  • 1
  • 16
  • 35