How to get all the user_id from Session

Using session can record users’ id when users log in, like
session[:user_id].

If I want to know how many users are online, so how can I get all the
users’s id from session?

Thanks!

On 31 Mar 2008, at 13:17, Bob Song wrote:

Using session can record users’ id when users log in, like
session[:user_id].

If I want to know how many users are online, so how can I get all the
users’s id from session?

Depending on what session store you are using this ranges from fiddly
to impossible. With the cookie store for example (the default since
rails 2) the session is stored client side, so this is impossible.
With other ones you’d have to load each session (while somehoe working
out which ones are just dead/expired).

Fred

On 31 Mar 2008, at 14:34, Frederick C. wrote:

out which ones are just dead/expired).
Indeed, a user count using sessions is always a guess. The only way to
have a reliable user online count, is by using a push server like
juggernaut for example. And that’s going to get you into trouble if
you want to deploy on a shared hosting account (since you have to run
a separate push server).

Best regards

Peter De Berdt

What they said. :slight_smile:

The “how many users are online” question is too relative anyway.
Visiting a web page is a single, transactional action (most of the
time) so you have:

at XYZ user ABC requested page 123 and it was returned.

So is that user “online”? What about 5 minutes after XYZ? are they
still “online”? what about 5 hours or 5 days? Do you see the conundrum
here?

On the other hand, if you set a threshold for yourself, like: “online”
means last activity within the past 1 hour, and then set a
“last_activity_at” datetime field on the user table, and then have an
after_filter that updates that field for each user action.

Now, you have a way to query by showing all users where
last_activity_at is within 1 hour ago. It’s still fuzzy because
“online” is a bad way to think about visitors to a web app. But for
some apps, like campfire for example, the domain of the app makes it a
reasonable question. I.e. if your last comment was within X minutes
and you haven’t explicitly logged out, then you are still “online”.
But after Y minutes you are “idle” and after Z minutes you are
“offline”. You have to define waht those mean for your app, though.

Just some thoughts.

-Danimal