# How to get all the user\_id from Session

**URL:** <https://www.ruby-forum.com/t/how-to-get-all-the-user-id-from-session/133423>\
**Category:** Rails\
**Created:** [March 31, 2008, 2:17pm UTC](https://www.ruby-forum.com/t/how-to-get-all-the-user-id-from-session/133423 "2008-03-31T14:17:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [March 31, 2008, 2:17pm UTC](https://www.ruby-forum.com/t/how-to-get-all-the-user-id-from-session/133423/1 "2008-03-31T14:17:51Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [March 31, 2008, 2:35pm UTC](https://www.ruby-forum.com/t/how-to-get-all-the-user-id-from-session/133423/2 "2008-03-31T14:35:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [March 31, 2008, 4:14pm UTC](https://www.ruby-forum.com/t/how-to-get-all-the-user-id-from-session/133423/3 "2008-03-31T16:14:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [March 31, 2008, 5:28pm UTC](https://www.ruby-forum.com/t/how-to-get-all-the-user-id-from-session/133423/4 "2008-03-31T17:28:45Z")

</div>

What they said. 🙂

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
