# Knowing when a user is logged in

**URL:** <https://www.ruby-forum.com/t/knowing-when-a-user-is-logged-in/99819>\
**Category:** Rails\
**Created:** [July 25, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/knowing-when-a-user-is-logged-in/99819 "2006-07-25T19:07:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [July 25, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/knowing-when-a-user-is-logged-in/99819/1 "2006-07-25T19:07:35Z")

</div>

I am looking into creating a website similar to a chat program. I know  
when the user logs in. However, the user can log out any of the  
following ways:

Logout link - This one is easy to handle  
User closer browser window  
User hits the back button  
User loses his/her internt connection.

Is there a way within rails to handle the latter three user actions?

Thanks!

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [July 25, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/knowing-when-a-user-is-logged-in/99819/2 "2006-07-25T23:46:27Z")

</div>

Usually, the last three cases are handled with a timeout of some  
kind. If the user’s browser doesn’t interact with the server at all  
in a specific amount of time, then the server expires the session.

Tim

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [July 26, 2006, 1:13am UTC](https://www.ruby-forum.com/t/knowing-when-a-user-is-logged-in/99819/3 "2006-07-26T01:13:43Z")

</div>

How do I know when the session expires? In the case of a chat room, I  
would have to update the database or something so other people in the  
room see that that person is no longer in the room.

Chris

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [July 26, 2006, 7:28am UTC](https://www.ruby-forum.com/t/knowing-when-a-user-is-logged-in/99819/4 "2006-07-26T07:28:55Z")

</div>

You might want to use railscron to expire sessions that have not been  
updated in 20 minutes.

```
        - dan

```

–  
Dan K. [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.dankohn.com/](http://www.dankohn.com/)tel:+1-415-233-1000

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [July 26, 2006, 8:33am UTC](https://www.ruby-forum.com/t/knowing-when-a-user-is-logged-in/99819/5 "2006-07-26T08:33:28Z")

</div>

A simple solution to expire sessions is to set expiry in controllers.  
E.g.,

class MyController  
session :session\_expires =\> 2.minutes.from\_now  
end

## While nice, this is not a great way to keep a “who’s online” list. It’s probably safest to keep an online status in the users table, and update it to Time.now each time a user browses to a new page. That way “who’s online” resolves to User.find(:conditions =\> [‘last\_hit\_server\_at’ \> ?', 2.minutes.ago]).

View this message in context:  
[http://www.nabble.com/Knowing-when-a-user-is-logged-in-tf2000253.html#a5498376](http://www.nabble.com/Knowing-when-a-user-is-logged-in-tf2000253.html#a5498376)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).
