cable icon indicating copy to clipboard operation
cable copied to clipboard

Helper methods for getting channel sizes

Open jwoertink opened this issue 3 years ago • 2 comments

I'm not sure if the rails one has this. I tried searching and couldn't find it, but I feel like I should have an easy way to know exactly how many connects are open on a specific channel.

# no clue what this actually looks like
ChatChannel.fetch_count("room:1") # => 35
EventChannel.fetch_count("events:4") # => 42

jwoertink avatar Apr 17 '23 19:04 jwoertink

I remember when monitoring redis for rails actioncable, it uses an internal key as a control pubsub, so it can do some maintenance tasks (like disconnecting all connections for a give client), could take advantage of similar approach, but I'm not confident about how to use it

or maybe could use a redis key to inc/dec on conn/disconn and fetch this number? 🤔

fernandes avatar Apr 17 '23 21:04 fernandes

Just for some added context here, what I'm currently doing is something like this:

class ChatChannel < Channel
  def subscribe
    increment_count
    stream_from("room:1")
  end
  def unsubscribe
    decrement_count
  end
end

Then elsewhere I have a loop that runs every minute asking what that count is. It would be so much nicer if I could just ask the channel how many are in there, and who they are. Then I wouldn't have to store that separately.

jwoertink avatar Apr 17 '23 21:04 jwoertink