Home > Rails > Rails Track Online Users through Session

Rails Track Online Users through Session

January 28th, 2008

We recently needed to implement tracking which users are online at our site. Matt Beedle posted a good starting guide to how to do this, but I found that there were a possible improvements from his tutorial.

Like Matt, we’re using an ActiveRecord session store to track who’s online when. Unlike Matt, we wanted to keep track of when the user last accessed a page, and what page they accessed. The most SQL-efficient way I could find to do this was to add fields directly to the session model and update those. To add the fields to the model, I just created a migration that added the stuff I wanted to track:

add_column :sessions, :user_id, :integer
add_column :sessions, :last_url_visited, :string

Next, I created a before_filter in our application to update these as the user moved through the site, like so:

session.model.update_attribute(:user_id, session[:user_id])
session.model.update_attribute(:last_url_visited, request.url)

With these properties getting updated, all that remained was to write a query to figure out who’s been moving around the site recently. This ended up being as simple as:

online_sessions = CGI::Session::ActiveRecordStore::Session.find( :all,
:select => "user_id, last_url_visited",
:conditions => [ "updated_at > ? and user_id is not null", Time.now() - 30.minutes ],
:limit => 50 )

And that was about it. The tracking is just one SQL UPDATE a pop, and the session find doesn’t need any fancy joins and such, so it ought to scale pretty well.

Bill Rails

  1. sacredceltic
    March 7th, 2009 at 10:47 | #1

    Not a good solution since exposing the user_id in the session constitutes a security breach…
    Better add fields on the user model…

  2. March 7th, 2009 at 11:53 | #2

    Hi,

    What session store are you using ? With the cookie based store, session is a Session::AbstractStore::SessionHash, and therefore, session.model fails.

    Regards,

  3. EpochWolf
    April 18th, 2009 at 10:02 | #3

    @Jean-Etienne Durand This only works with ActiveRecordStore.

  4. shyam
    August 18th, 2009 at 00:03 | #4

    The link you posted http://matt-beedle.com/2006/12/13/rails-how-to-find-out-who-is-online/ doesn’t exist.I need a method to track number of online users in my rails application.
    I am using
    config.action_controller.session = {
    :session_key => ‘_myapp.com_session’,
    :secret => ‘fgdg8sdfds0dsfdf3r3′
    }

  5. October 2nd, 2009 at 10:56 | #5

    Why add to the session table? If your goal is to find out who is online and what url they last visited why not just add a last_url field to the user model and update that in your before filter, then do something like

    User.find :conditions => ["updated_at > ?", Time.now - 1.hour]

  6. October 2nd, 2009 at 11:21 | #6

    I should mention that if you do this and you’re doing any clever after_save filters it’s important to optimize them. I was generating a profile header image with Image Magick on each save which was making things rather slow.

  1. No trackbacks yet.