I have made a simple realtime visitor counter.
You can download it from this repository.
What happens is that disconnect event (even after browser closing) on se
From Socket.IO 1.0 the io.engine.clientsCount
property is available. This property tells you how many open connection does your app currently have.
io.sockets.on('connection', function (socket) {
io.sockets.emit('count', {
number: io.engine.clientsCount
});
socket.once('disconnect', function () {
io.sockets.emit('count', {
number: io.engine.clientsCount
});
});
});
Note: Use .once
instead of .on
and the listener will be removed automatically from the socket
what is good for us now, because the disconnect event is only fired once per socket.