socket.io: disconnect event isn’t fired

Put your on disconnect code inside your on connect block and edit it a bit like so:

io.sockets.on('connection', function (socket) {
    count++;
    io.sockets.emit('count', {
        number: count
    });

    socket.on('disconnect', function () {
        console.log('DISCONNESSO!!! ');
        count--;
        io.sockets.emit('count', {
            number: count
        });
    });
});

This way you’re detecting when a specific socket (specifically the socket you pass to your anonymous function that is run on connection) is disconnected.

Leave a Comment