Real-Time Online Status

I am looking for a better way to track a User’s Online Status so I can set an “Online Status Indicator” like you would see in an Instant Messaging Application.

Ideally I would like to have 3 statuses:[INDENT]- Online and Active (green)

  • Online but Idle (yellow)
  • Offline (grey)[/INDENT]

Currently I set $_SESSION[‘loggedIn’] = TRUE; in my “log_in.php” script and then maintain that status by using session_start(); at the top of each page.

As far as I know, my current approach wouldn’t always give you accurate readings like if a User is “idle” or if they leave my site.

Any ideas??

Thanks,

Debbie

Is the session data currently being stored in the file system or in a database table?

I have been saving it in the User’s SESSION, but I just added some code so I have loggedIn and lastActivity stored in the User’s record in the database.

Debbie

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.

  1. Server side. Every time the user goes to a new page, or submits a form, you maintain the user as active.

  2. Client side. Use javascript to check for mouse movement and keyboard activity, update the server every minute or so via AJAX.

Since I don’t plan on learning or touching JavaScript in 2012, can I just do this server-side and have it be decent?

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!!)

So…

  • 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.

And even if a Poster is idle, that will show up as long as the Reader is not idle!!! (If a Reader is taking an hour to read my Article, then the Online Statuses will be an hour old until the Reader refreshes things (since there is no JavaScript option).

Comments?

Thanks,

Debbie

Hi Debbie,

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.

Steve

Don’t you think that is good enough?

I mean if a Reader is sitting idle for an hour, then most people would understand that the page they are viewing is an hour old and may not reflect new updates.

When I learn AJAX I can make it more interactive, but I’m thinking what I said above should be “dynamic” enough for now…

What do you think?

Debbie

Hi Debbie,

I think that it can be good enough, especially once your users learn how this works they will understand they have to do a page refresh. Some users will ‘gripe’, “This is so old fashion” others won’t care, and some will appreciate that you don’t have any JavaScript. When you are ready to move to AJAX then you can replace this logic using good JavaScript technique.

Steve