Hi,
I’m using Restful Authentication plugin to provide credentials to users
to my service.
I’d like to be able to list which users are logged into the system. What
would be the best approach.
At least in the sessions controller I could add a flag for each user
when they come and go but that means DB action and I’m not sure if
that’s the proper way.
I found this also:
http://matt-beedle.com/2006/12/13/rails-how-to-find-out-who-is-online/
But I’d like to know your opinion about the best way.
Thanks.
Hi, I also found another tutorial where basically was updating a status
flag for each user.
http://blog.levicole.com/articles/category/ror
Regards
Hi again,
I’ve been checking more about sessions and all that. I haven’t checked
every single detail of all possible ways but I think that cokies can be
used, activeRecordStore, etc.
As said I’m using Restful authentication and that goes with cockies. Ok
with me so far.
I’ve added a column to my users to store their status. I update it when
they come and go. But of course users don’t logout when the close their
browsers. So I would register some actions when users browse the site,
i.e. record the timestamps.
After doing that I would use a script runner to see if the last activity
was for example 30 minutes ago, if so then I would update their status
to logged out.
Do you think that makes sense?
Btw, is there anything I should clean up from the sessions that Restful
Auth is creating? Where is the garbage found?
Cheers!
Great so that’s another example. Thanks.
In the end I actually decided to use active record store and it is going
pretty well. But I have some questions about and I’ll start a new topic
about sessions cleanup and avoiding duplication.
Cheers
Hi, I also found another tutorial where basically was updating a status
flag for each user.
In that direction (storing each user’s last_visit time in the User
model) :
Parked at Loopia
remark: it only counts the logged-in users.
How to detect who is online now/was active during the last 5 minutes?
in brief:
- add a last_visit field to your User model, and
- have controllers update it once per request.* find who’s online by
comparing users.last_visit to 5.minutes.ago
in long :
Example: (where you use some for of
AuthenticatedSystem to check if the user is logged_in?)
migration
class AddLastVisitToUser < ActiveRecord::Migration
def self.up
add_column :users, :last_visit, :datetime
add_index :users, :last_visit
end
def self.down
remove_column :users, :last_visit
remove_index :users, :last_visit
end
end
model : user.rb
class User << AR
…
def record_last_visit
ActiveRecord::Base.connection.execute(“update users set last_visit =
now() where id = #{id}”)
end
…
def online?(max_delay=5.minutes)
last_visit && (Time.now - last_visit < max_delay)
end
view: who is online now/was active in the last 5 minutes
Who's online
<%= User.find(:all, :conditions => ["last_visit >=
?",5.minutes.ago]).collect{|u|
link_to( u.login_or_name, user_path(:id=> u))}.join(' - ') %>
controllers
class ApplicationController < ActionController::Base
include AuthenticatedSystem
…
end
module AuthenticatedSystem
protected
def logged_in?
…
@current_user.record_last_visit unless @last_visit_already_recorded
@last_visit_already_recorded = true
true
end
…
end
Alain