Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you implement a "last seen" feature for users?

On Stack Overflow, the profile page lists a "last seen" property. This doesn't seem to be updated on every page view (for performance reasons, obviously). How would you implement it in a heavy-traffic web app? Would you update it only on certain pages? Or cache the last time you logged the user's last visit and wait a specific amount of time before updating the database? Or something completely different?

like image 489
Kevin Pang Avatar asked Jan 22 '09 14:01

Kevin Pang


3 Answers

On a heavy-traffic site like Stack Overflow, I would only update the "last seen" variable when a user actually does something. Lurking around and reading questions and answers shouldn't count as a user being "seen" by the system. Asking and answering questions, or voting on them should be actions that update when a user is last seen.

I won't talk about the implementation details because that's already covered by other answers (and I would probably get it wrong).

like image 141
Bill the Lizard Avatar answered Oct 26 '22 03:10

Bill the Lizard


You'll probably find "What strategy would you use for tracking user recent activity?" to be helpful. The issues are similar.

like image 26
Brian Avatar answered Oct 26 '22 05:10

Brian


I would use a SESSION. And only set it the first visit of the session. Also resetting it every hour or so for if people leave the browser open. In php something like this:

if(!isset(!_SESSION['lastSeen'])){
 $_SESSION['lastSeen'] = time();
 updateLastSeenInDatabaseOrSomething();
}
else{
 if($_SESSION['lastSeen'] < time() + 2 * 60 * 60){ //2 hours
  $_SESSION['lastSeen'] = time();
  updateLastSeenInDatabaseOrSomething();   
 }
}

Something like that but then with OO and not doing the same thing twice.

like image 27
Pim Jager Avatar answered Oct 26 '22 03:10

Pim Jager