Using Devise on Rails, is there some way to list all the users who currently have active sessions i.e. the users that are currently logged in?
Ps. I'm looking for a robust solution, not something simplistic like the ones in this question
Simply add after_filter in ApplicationController
after_filter :user_activity
private
def user_activity
current_user.try :touch
end
Then in user model add online? method
def online?
updated_at > 10.minutes.ago
end
Also u can create scope
scope :online, lambda{ where("updated_at > ?", 10.minutes.ago) }
https://github.com/ctide/devise_lastseenable
You can use this gem that I wrote to store the 'last_seen' timestamp of a user. From there, it's pretty trivial to display the users who were last_seen in the last 5 or 10 minutes.
If you're bothered by making a trip to database on every. single. http. request. only to have some small window where you can sort of assume that a user is online; I have an alternate solution.
Using websockets and redis it's possible to reliably get a user's online status up to the millisecond without making a billion costly writes to disk. Unfortunately, it requires a good bit more work and has two additional dependencies, but if anybody's interested I did a pretty detailed write here:
How do I tell if a user is online?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With