21

I have seen ActionCable.server.open_connections_statistics, ActionCable.server.connections.length, ActionCable.server.connections.map(&:statistics), ActionCable.server.connections.select(&:beat).count and the like, however this is only "per process" (server, console, server worker, et cetera). How do I find out everyone who is subscribed to ActionCable at this time? This should return the same value on any Rails process in each environment (development, staging, production). So for example, in development console you can also see the connections on the development server since they, in theory, use the same subscription adapter (redis, async, postgres).

Rails 5.0.0.beta3, Ruby 2.3.0

related ActionCable - how to display number of connected users?

Community
  • 1
  • 1
b264
  • 334
  • 1
  • 2
  • 11

2 Answers2

17

If using redis you can see all the pubsub channels.

[2] pry(main)> Redis.new.pubsub("channels", "action_cable/*")
[
    [0] "action_cable/Z2lkOi8vbWFvY290LXByb2plL3QvUmVzcG9uXGVyLzEx",
    [1] "action_cable/Z2lkOi8vbWFvY290LXByb2plL3QvUmVzcG9uXGVyLzI"
]

This will show all websocket connections for all the Puma workers together. And if you have multiple servers it will probably show those here too.

Razor
  • 719
  • 8
  • 18
  • 1
    This is _exactly_ what I was looking for; thank you. – b264 Mar 28 '16 at 03:53
  • 4
    Is there a way to diferenciate them , like by room or some? – plombix Sep 06 '16 at 11:16
  • @plombix please check my answer below –  Nov 13 '16 at 11:19
  • 3
    @LoaiGhoraba I don't see an answer below. Did you have an answer somewhere else? – Matt Jan 06 '17 at 19:02
  • 9
    Just so you know: You can decode64 the string (after the slash of course) and get something like this: `gid://myproject/User/250581` This string depends on what you use for `identify_by` I guess. – 2called-chaos May 24 '17 at 11:57
  • 4
    `Redis.new(url: 'redis://:auth_code@ip:port/db_number').pubsub('channels', 'action_cable/*').map { |c| Base64.decode64(c.split('/').last) }` – Evmorov Sep 28 '17 at 12:14
  • 4
    Use `ActionCable.server.pubsub.send(:redis_connection)` to get `ActionCable`'s Redis connection (instead of creating on your own), and `ActionCable.server.pubsub.send(:channel_with_prefix, "action_cable/*")` for the correct channel name – Oded Niv Nov 08 '17 at 12:18
16

To be more specific for ActionCable (and to Redis)...

Assuming this channel:

class RoomChannel < ApplicationCable::Channel
end

Get the Redis adapter from ActionCable instead of creating it yourself (you'd need to supply the URL from config/cable.yml otherwise):

pubsub = ActionCable.server.pubsub

Get the channel's name including channel_prefix you may have specified in config/cable.yml:

channel_with_prefix = pubsub.send(:channel_with_prefix, RoomChannel.channel_name)

Get all connected channels from RoomChannel:

# pubsub.send(:redis_connection) actually returns the Redis instance ActionCable uses
channels = pubsub.send(:redis_connection).
  pubsub('channels', "#{channel_with_prefix}:*")

Decode the subscription name:

subscriptions = channels.map do |channel|
   Base64.decode64(channel.match(/^#{Regexp.escape(channel_with_prefix)}:(.*)$/)[1])
end

If you are subscribed to an ActiveRecord object, let's say Room (using stream_for), you can extract the IDs:

# the GID URI looks like that: gid://<app-name>/<ActiveRecordName>/<id>
gid_uri_pattern = /^gid:\/\/.*\/#{Regexp.escape(Room.name)}\/(\d+)$/
chat_ids = subscriptions.map do |subscription|
  subscription.match(gid_uri_pattern)
  # compacting because 'subscriptions' include all subscriptions made from RoomChannel,
  # not just subscriptions to Room records
end.compact.map { |match| match[1] }
Oded Niv
  • 2,557
  • 2
  • 22
  • 20
  • NoMethodError (undefined method `channel_with_prefix' for #) – Favourite Onwuemene Apr 23 '19 at 04:16
  • 2
    @FavouriteOnwuemene are you using Redis? I wrote this answer is specifically for Redis. I can see that this method is still there in Rails 5.2.3 (https://github.com/rails/rails/blob/v5.2.3/actioncable/lib/action_cable/subscription_adapter/channel_prefix.rb), but ChannelPrefix is included in redis.rb and not in async.rb or postgresql.rb. – Oded Niv Apr 24 '19 at 15:28