I have a webpage which I want to display users who have that page opened. (I'm using node.js for server side)
My current attempt:
On server side, there's a counter that counts the amount of time he submits the GET request to that webpage. Hence if he opens that page more than once, counter++
Everytime the user leaves the webpage, it sends a websocket to the server using socket.io.
window.onbeforeunload = function() {
socket.emit('ClosePage');
}
Then on the server side, when it receives this socket, it decreases his counter by 1, a.k.a counter--
So using this logic, it's possible to detect when an user has the webpage opened and when he actually closes the webpage for real.
HOWEVER, the problem occurs when an user refreshes the page like crazy, the server will just adds up continuously the counter without decreasing it afterward, because the window has not unloaded yet (my guess).
Does anyone know how to fix stuffs like this? Or does anyone know what algorithm does most forums use when they display users or strangers who are reading a post?
Thanks a lot!