Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find out who is connected to ActionCable?

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?

like image 963
b264 Avatar asked Mar 19 '16 20:03

b264


People also ask

What is ActionCable?

1 What is Action Cable? Action Cable seamlessly integrates WebSockets with the rest of your Rails application. It allows for real-time features to be written in Ruby in the same style and form as the rest of your Rails application, while still being performant and scalable.

What is AnyCable?

AnyCable allows you to use any WebSocket server (written in any language) as a replacement for your Ruby server (such as Faye, Action Cable, etc). AnyCable uses the same protocol as ActionCable, so you can use its JavaScript client without any monkey-patching.

How do you test an action cable?

Testing Action Cable To test this connection class, we can use connect method to simulate a client server connection and then test the state of connection is as expected. The connection object is available in the test.


2 Answers

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.

like image 94
Razor Avatar answered Sep 25 '22 09:09

Razor


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] }
like image 37
Oded Niv Avatar answered Sep 23 '22 09:09

Oded Niv