News for hackers. Stuff that matters.
Re:Show HN: Real Time Users – Add a real-time user counter t... by nevi-me on Thursday June 09, 2016 @12:47PM (#11870567)
Why not just use websockets? I use socket.io on my site. I keep track of socket IDs in memory, if a connection is lost, I get a disconnect event, and vice versa. Socket events are sent to users when the number changes.

I manage the frequency of socket emits using RxJS, to prevent sending too many changes to users.

https://movinggauteng.co.za, there's a counter at the top of the page.

EDIT: Only saw a comment about why the OP used Ajax after posting this.

Re:Show HN: Real Time Users – Add a real-time user counter t... by simplify on Thursday June 09, 2016 @08:43PM (#11873480)
Websockets would overkill for this kind of feature – you don't need realtime two-way communication. Server sent events (SSE) [1] are a much better alternative; it's a simple, long-lived HTTP request where the server can push data to the client. They're built into most browsers [2], and the client-side polyfill [3] is small (2kb gzipped).

[1] https://developer.mozilla.org/en-US/docs/Web/API/Server-sent...

[2] http://caniuse.com/#search=server-sent%20events

[3] https://github.com/Yaffle/EventSource

Re:Show HN: Real Time Users – Add a real-time user counter t... by nevi-me on Thursday June 09, 2016 @11:39PM (#11874141)
Fair enough, but what happens if I open a page, send an Ajax call to say "add 1 user", then promptly close that page? I don't know what OP is doing to detect that a user is no longer on the site.

The reasons why I used WebSockets are that I get a disconnect event immediately, and I was already using WebSockets for other features on the site

Re:Show HN: Real Time Users – Add a real-time user counter t... by simplify on Friday June 10, 2016 @02:38PM (#11878797)
It's a long-lived HTTP request. The server automatically knows if a client disconnects; that's a feature built into all HTTP server libraries everywhere.

But yes, if you're already using websockets for other features, there's no reason not to continue to use them, as you explained :)

Re:Show HN: Real Time Users – Add a real-time user counter t... by WWKong on Thursday June 09, 2016 @08:05PM (#11873326)
Can I get your help in deploying socket.io to Google App Engine? Is it possible? It works locally for me but Google app engine seems to close out the websocket.

http://stackoverflow.com/questions/37401250/how-do-i-deploy-...