Tracking online users with timestamps

February 3, 2020

Track your progress

Sign in to track your progress and access subscription-only lessons.

Log In

Your Teacher

Hi, I'm Chris. I'm the creator of GoRails, Hatchbox.io and Jumpstart. I spend my time creating tutorials and tools to help Ruby on Rails developers build apps better and faster.

About This Episode

We can use timestamps in our database to track of online users in your Rails

Notes

Keeping track of users who are "online" in your application can be done in several different ways.

This episode, we're going to implement a simple version of online user tracking by storing a timestamp of the last request the user has made. We can estimate which users are online by simply querying for any users who are online in the last 5 minutes. Depending on how accurate we want to be, we can adjust this window to be 1 minute or 15 minutes to be more or less accurate.

No matter what you do, you'll end up with an estimation here. The user could leave a tab open in their browser for 8 hours and they would be considered "offline" after 5 minutes.

Next episode, we'll talk about using ActionCable and having a websocket provide realtime updates of user's online status.

Resources

Want to stay up-to-date with Ruby on Rails?

Join 86,796+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.