Online users and sessions - logic help

Hello G.s,

I have an application which lets users login . In order to see how many
users are online (active in the last 20 mins) I have a before_filter on
every page which updates a column in the user table called
“last_hit_server”

This is a little silly and is really slowing things down.

Here are my aims:

• I need to create a list of all the users active in the last 20 mins
• I need to be able to see when a user was ‘online’

Obviously I need to start tracking sessions, but I really need some help
with the logic of it

Here is what I’m thinking so far…


  • When they log in I first check to see if there is a session already
    assigned to that user
    • (If there is – I destroy it)
  • I then create a new session and assign the user id to it.

“Who’s online”: I can then pull out a list of all the sessions which
have been updated in the past 20 mins along with the user id

When they log out, I destroy the session


But this doesn’t solve my problem of seeing when a person was last
‘online’ if I have destroyed the session

Any suggestions?

You’ve already said that you destroy sessions when they log in if the
session still exists… so just don’t destroy the session when someone
logs
out. You’re left with an idle session that exists until the next time
they
log in this way, but you can always set up some kind of scheduled thing
(varies depending on OS) that will go in and clean up any sessions that
haven’t had activity in amount of time.