cable-cr / cable

It's like ActionCable (100% compatible with JS Client), but you know, for Crystal
MIT License
129 stars 12 forks source link

Helper methods for getting channel sizes #66

Open jwoertink opened 1 year ago

jwoertink commented 1 year ago

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
fernandes commented 1 year ago

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? 🤔

jwoertink commented 1 year ago

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.