socket.io: why socket.disconnect() on client side doesn't fire disconnect event on the server?

here’s my code:

client-side

 var socket = io.connect('http://' + serverAddress ,{ reconnection: false, forceNew: true  }  );

 socket.emit('monitorAddedNotify' , { video: sourceVideo , socketId: socket.id});

 socket.on('disconnectThatSoc', function(){
    socket.disconnect();
});

server-side:

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


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

the socket.disconnect() method doesn’t work why?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 27 (4 by maintainers)

Most upvoted comments

are you dumb? you are waiting for an emit on client but no emit on server. fuck out of here

That’s some real negative energy, Ehsan666x.

Maybe to manually disconnect the client, you can use a function with some emitting. I don’t know if this’ll help, but I think I got something… Maybe…

Client:

function ManualSocketDisconnect() {
    socket.emit("manual-disconnection", socket.id);
    
    socket.close();
    
    console.log("Socket Closed. ");
}

Server:

io.on("connection", function(socket) {
    console.log("User " + socket.id + " Connected. ");
  
    socket.on("manual-disconnection", function(data) {
        console.log("User Manually Disconnected. \n\tTheir ID: " + data);
    });
});

Sorry, but I probably left some holes in my code. I’m no professional. 🙁

I handled this problem this way. I’ve made an emit sender on client which is calling heartbeat on server.

socket.on("heartbeat", function() {
            // console.log('heartbeat called!');
            hbeat[socket.id] = Date.now();
            setTimeout(function() {
                var now = Date.now();
                if (now - hbeat[socket.id] > 5000) {
                    console.log('this socket id will be closed ' + socket.id);
                    if (addedUser) {
                        --onlineUsers;
                        removeFromLobby(socket.id);
                        try {
                            // this is the most important part
                            io.sockets.connected[socket.id].disconnect();
                        } catch (error) {
                            console.log(error)
                        }
                    }
                }
                now = null;
            }, 6000);
        });

I found this code function to call:

io.sockets.connected[socket.id].disconnect();

Try to enter page and fast reload by pressing many times F5 button.

hahaha wow i can’t believe i was on this thread 4 years ago

This still happens. It doesn’t have to be F5/refresh. Opening a tab and closing it soon quickly at a certain stage, causes disconnect to not be triggered. It’s hard to reproduce because sockets are very fast, especially when running locally. I noticed opening the tab in the background and having other tabs open helped to reproduce the problem (locally). I think this is an important bug that should be addressed.