如何查找谁连接了ActionCable?

21
我看到了ActionCable.server.open_connections_statisticsActionCable.server.connections.lengthActionCable.server.connections.map(&:statistics)ActionCable.server.connections.select(&:beat).count等内容,但这只是“每个进程”的(服务器、控制台、服务器工作者等等)。那么如何查找此时订阅ActionCable的所有人呢?这应该在各个环境(开发、暂存、生产)的任何Rails进程中返回相同的值。例如,在开发控制台中,您也可以看到开发服务器上的连接,因为它们理论上使用相同的订阅适配器(redis、async、postgres)。

Rails 5.0.0.beta3,Ruby 2.3.0

相关文章:ActionCable - 如何显示已连接用户的数量?

2个回答

17
如果使用redis,您可以查看所有的发布和订阅频道。
[2] pry(main)> Redis.new.pubsub("channels", "action_cable/*")
[
    [0] "action_cable/Z2lkOi8vbWFvY290LXByb2plL3QvUmVzcG9uXGVyLzEx",
    [1] "action_cable/Z2lkOi8vbWFvY290LXByb2plL3QvUmVzcG9uXGVyLzI"
]

这将显示所有 Puma 工作进程的 WebSocket 连接。如果您有多个服务器,它可能也会在此处显示。


4
有没有办法区分它们,比如按房间或其他方式? - plombix
3
@LoaiGhoraba,我没有看到下面有答案。你是否在其他地方有答案? - Matt
9
知道了:你可以对斜杠后面的字符串进行base64解码,得到类似于这样的结果:gid://myproject/User/250581。我猜这个字符串的具体形式取决于你在identify_by中使用的是什么。 - 2called-chaos
4
Redis.new(url: 'redis://:auth_code@ip:port/db_number').pubsub('channels', 'action_cable/*').map { |c| Base64.decode64(c.split('/').last) } - Evmorov
4
使用 ActionCable.server.pubsub.send(:redis_connection) 获取 ActionCable 的 Redis 连接(而不是自己创建),使用 ActionCable.server.pubsub.send(:channel_with_prefix, "action_cable/*") 获取正确的频道名称。 - Oded Niv
显示剩余2条评论

16
更具体地说,针对ActionCable(以及Redis)......
假设存在这个频道:
class RoomChannel < ApplicationCable::Channel
end
从ActionCable获取Redis适配器,而不是自己创建它(否则需要提供来自config/cable.yml的URL)。
pubsub = ActionCable.server.pubsub
获取频道的名称,包括您可能在 config/cable.yml 中指定的频道前缀:
channel_with_prefix = pubsub.send(:channel_with_prefix, RoomChannel.channel_name)

获取RoomChannel中的所有已连接通道:

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

解码订阅名称:

subscriptions = channels.map do |channel|
   Base64.decode64(channel.match(/^#{Regexp.escape(channel_with_prefix)}:(.*)$/)[1])
end
如果您已经订阅了一个ActiveRecord对象,比如说使用stream_for订阅了Room,那么您可以提取ID:
# 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] }

NoMethodError (undefined method `channel_with_prefix' for #ActionCable::SubscriptionAdapter::Async:0x00007f2b20a45d88) - Favourite Onwuemene
2
@FavouriteOnwuemene 你在使用Redis吗?我写这篇答案是专门针对Redis的。我可以看到这个方法仍然存在于Rails 5.2.3中(https://github.com/rails/rails/blob/v5.2.3/actioncable/lib/action_cable/subscription_adapter/channel_prefix.rb),但ChannelPrefix被包含在redis.rb中,而不是async.rb或postgresql.rb中。 - Oded Niv

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接