0

Asynchronous in Socket.io

I use 2 variables online (to count the number of online people currently) and total variable (count the total number of people visited). The problem is that when I reload page continuously online variable has not decreased, it has increased.

io.on('connection', function(sock) {

    sock.on('disconnect', function() {
        sock.broadcast.emit('Client-disconnect', --online);
    })
    io.sockets.emit('Client-connection',{ onl: ++online, tol: ++total });
}
3
  • 1
    that suggests sock.on('disconnect' isn't called - can you add some debugging to test this Commented Aug 20, 2017 at 8:41
  • I think it did not catch up because I reloaded continuously Commented Aug 20, 2017 at 8:45
  • 1
    The Best Explanation for your query: stackoverflow.com/questions/14220321/… Commented Oct 23, 2017 at 11:53

1 Answer 1

1

The connections are being left open as they are not closed when the page is either refreshed or closed. Listen to the page events to be able to close the connection before the page is removed:

window.onbeforeunload = function(e) { sock.disconnect(); };

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.