It requires 2 layers. You can use just one or the other, but both will give a more real time reading. However realize that the internet is a stateless system, so the only way the server knows, is if the client sends information back.
Server side. Every time the user goes to a new page, or submits a form, you maintain the user as active.
Right now I write to my database when a User Logs In or Logs Out. I update “logged_in” to “0” or “1” and “last_activity” gets NOW().
To back up a minute, what I am doing is this…
My website is full of Articles. Beneath each Article can be Comments. Next to each Comment, is the Poster’s Username, Online Status, Photo, Location, and Comment.
I want the Poster’s Online Status to entice Readers to post a Comment. (The logic being that if people think/see that others are online and Commenting, then they might be more inclined to do the same!!)
If a Poster logs IN or OUT then their Online Status is updated
If a Reader goes to an Article/Comments page, then the Reader will see a “snap-shot” of Posters who were On/Offline at that moment (sorta)
I am trying to add code that will update the Online Status every time a Poster navigates to another page.
Once a page loads and a user sits idle for a long time then without using AJAX you will not be able to keep this fresh, until the user loads a new page on your site, which at that point you could refresh everyone’s status from querying active users in the Db.