How to track online users in Rails?
Asked Answered
D

1

6

This was always a problem for me, as rails doesn't have some mechanisms for session tracking like java frameworks do, what methods do you use to track who is currently logged on your site? I use a simple method by setting up last_visited field with current time every time user clicks somewhere on the site, and then checking for users which have updated that field in (lets say) last 10 minutes. What other methods are there besides this one?

Donitadonjon answered 25/2, 2009 at 15:35 Comment(0)
S
6

If you are using the active_record_store for your sessions, you can try something like this:

sessions = CGI::Session::ActiveRecordStore::Session.find(:all, :conditions => ["updated_at >= ?", 15.minutes.ago], :order => "created_at ASC")
sessions.each do |session|
  if session.data[:user]
    online_users << User.find(session.data[:user])
  end
end
Separatrix answered 25/2, 2009 at 17:50 Comment(2)
That's nice solution but I am using cookie based sessions.Donitadonjon
I certainly found this useful. In the latest rails 2.x versions you have to use ActiveRecord::SessionStore::Session.findJamilajamill

© 2022 - 2024 — McMap. All rights reserved.