Hacker News new | ask | show | jobs
by mikeaag 3659 days ago
Hey subie,

I'm one of the creators of Real Time Users.

Only 1 AJAX call get sent to 'track' you on the site for each page load. The AJAX calls you're seeing are asking the server for the latest number of users on the site so that we can update the value displayed.

This isnt the most efficient way of doing this, as if the site has a low number of users, the count will return the same number most of the time. A better way would be to use websockets, however i only have limited knowledge of using websockets so we decided it was best to just stick to the tech we knew (Especially as we built it in a weekend).

If people start using this more, we might update it to use websockets though.

Hope that answers your questions. Let me know if theres anything else you'd like to know.

3 comments

I commented separately, I use sockets for this. I don't know about libraries for PHP as I haven't used it in a while, but most socket libraries make it easy to implement something.

My implementation is in-memory, and the nice thing about it is that if fit example my Node.js app restarts, all active users will get disconnected, and they will emit a connection event, allowing me to get the count of online users again.

I can extract relevant code and share it if it'd help.

Hi Nevi-me

I've come across Socket.io before, and played around with pusher as well. And if i remember correctly, Laravel has built in helpers for sending websocket messages.

My main issue is actually running the node.js application, as its something i've not done before. That was another reason for not using websockets. Wanted to remove as many barriers that might slow us down or stop us from launching, so we stuck with what we knew.

I'm sure i'll update this at some point to use websockets though, gotta learn how to deploy node.js sites at some point :D

Ohh I didn't even look in depth at the request. Thanks for the response!

I'd say ajax is probably the right way as far as polling for the data goes. Using web sockets for polling incredibly simple data doesn't seem necessary.

The benefit of using websockets is that each user's browser doesnt need to constantly be requesting the number of users from our server. If the number of users on the site hasnt changed, then with AJAX polling, its a wasted request. But with websockets there is no waste, the client gets pushed the updates when they happen from the server.

Having said all that, with the number of requests that we're getting at the moment, i dont think it makes much of a difference. Though im guessing if we got a lot more traffic, then websockets would probably reduce our server load.

Are these users or visitors? I'm not sure how this implementation would be able to distinguish between a user and a visitor of our site.
It's currently unique visitors. So, if you refresh the page, it doesn't increment the counter, as it knows you've already been logged. So it's visitors, rather than visits.