Displaying User presence when tracking online Users with ActionCable
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 :)
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.