Ask A Question

Notifications

You’re not receiving notifications from this thread.

Displaying User presence when tracking online Users with ActionCable

Nathan Chalot asked in Rails

Hi,
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"
    ids.include?(id.to_s)
  end
  ...
end

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 :)

Reply

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

Reply

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.

Reply
Join the discussion
Create an account Log in

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

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

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