0

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!

user2567834
  • 41
  • 1
  • 5
  • Just googled and found some links. Here are couple of them [Link1](http://stackoverflow.com/questions/6741397/is-there-a-way-to-detect-if-the-user-has-pressed-the-refresh-button-using-jquery), [Link2](http://stackoverflow.com/questions/6407395/knowing-user-refresh-the-page-web-stats) – Nilesh Aug 15 '13 at 23:52

3 Answers3

0

You could drop a cookie and not increment the count if the cookie exists. Or you could block on IP, but that might knock out some genuine views.

Grim...
  • 16,518
  • 7
  • 45
  • 61
0

You're right about that regarding the delay before it decrements the value. This will be correlated with the connection timeout value in socket.io. You can't guarantee that every user will be using websockets. If they are using XHR-polling or other types of transport layers it will have to timeout before decrementing. Websocket users will cause the change immediately.

Hderms
  • 170
  • 1
0

Google analytics shows you who is on your site and what content they are viewing in real time.

Anthony Hildoer
  • 545
  • 3
  • 6