All threads / Displaying User presence when tracking online Users with ActionCable
Ask A Question


You’re not receiving notifications from this thread.

Displaying User presence when tracking online Users with ActionCable

Nathan Chalot asked in Rails

I'm wondering on my current implementation for tracking and displaying user online presence.
I followed "Realtime Online User Tracking with ActionCable" for the most part and right now I'm using the following code to show if a particular user is online or not:

class User < ApplicationRecord
  def is_online?
    ids = ActionCable.server.pubsub.redis_connection_for_subscriptions.smembers "online_users"

It's working fine but I'm wondering:

  • is it ok to make a request like this every time I display a user ?
  • or should I instead update a boolean field on the User model on subscribed and unsubscribed methods in the Channel.

Any advice would be of great help.
Thanks :)

Yup. It's actually going to be faster than querying your database because Redis is all in-memory.

I was wondering about that since that method is called once for each displayed user on the page so it seemed to me that it could cause a N+1 query like problem.
I'll keep it that way then :)
Thanks Chris.

Join the discussion

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

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

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

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.