动作电缆计数订阅

时间:2018-10-14 21:33:29

标签: ruby-on-rails websocket redis channel actioncable

在我的Ruby on Rails项目中,我有一个Tickets MVC。用于编辑故障单(例如ID为5的故障单)的典型页面为/tickets/5/edit

在另一页上,我有一个简单的表,我想实时显示每个票证正在查看多少用户(即,显示/tickets/1/edittickets/2/edit上有多少用户。) ..

每当用户登录并访问/tickets/:id/edit上的票证时,我都会看到一个咖啡脚本ticket_notifications.coffee,它看起来像:

$ ->
  if $('body').attr('data-controller') == 'tickets' && $('body').attr('data-action') == 'edit'
    ticketId = document.getElementById('ticket_id').value
    App.ticket_notifications = App.cable.subscriptions.create {channel: "TicketNotificationsChannel", ticket_id: ticketId},
      connected: ->
# Called when the subscription is ready for use on the server
      disconnected: ->
# Called when the subscription has been terminated by the server
      received: (data) ->

我的app/channels/ticket_notifications_channel.rb如下:

class TicketNotificationsChannel < ApplicationCable::Channel
  def subscribed
    # stream_from "some_channel"
    if current_user&.account
      stream_from "ticket_notifications_channel_#{current_user.account_id}"
      if params[:ticket_id]
        if Ticket.find(params[:ticket_id]).account == current_user.account
          ActionCable.server.broadcast "ticket_notifications_channel_#{current_user.account_id}",
                                       {stuff: "Agent #{current_user.email} is viewing ticket #{params[:ticket_id]}"}
        end
      end
    end
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end
end

前端表看起来像(我使用的是Slim,但类似于erb):

table.table.table-hover
            thead
              tr
                th
                  | Ticket #
                th
                  | Short Code
                th
                  | Mobile Number
                th
                  | Number of Tickets
                th
                  | Updated At
                th
                  | Viewers
            tbody
              - unless @tickets.empty?
                - current_time = Time.now
                - @tickets.each do |tkt|
                  - tkt_updated_at = tkt.updated_at
                  tr.m-unread.m-tr-clickable data-href=edit_ticket_path(id: tkt.id)
                    td
                      b #{tkt.id}
                    td
                      b #{tkt.short_code}
                    td
                      b #{tkt.mobile_number}
                    td
                      - if last_message = tkt.messages&.last&.body
                        b #{last_message[0..60]}
                    td
                      - if (current_time-tkt_updated_at) <= time_period
                        b #{time_ago_in_words(tkt_updated_at)} ago
                      - else
                        b #{tkt_updated_at.in_time_zone.strftime('%b %e %H:%M:%S %Y')}
                    td
                      b Count how many subscriptions to tickets_notifications channel with params tkt.id here. 

谢谢。

1 个答案:

答案 0 :(得分:1)

您可以检查以下问题的答案:

How do I find out who is connected to ActionCable?

基本上,您要做的。

Redis.new.pubsub("channels", "action_cable/*")

这将为您提供所有活动的pubsub频道。

此外,在创建新订阅时,您还可以提供userId以及ticketID

App.cable.subscriptions.create {channel: "TicketNotificationsChannel", ticket_id: ticketId, user_id: userId}

因此,Redis.new.pubsub("channels", "action_cable/*")现在将以以下方式为您提供所有活动的订阅

["action_cable/Z2lkOi8vYXBpL1VzZXIvMTUwMjIz", "action_cable/Z2lkOi8vYXBpL1VzZXIvMTUwNTc0"]

对上述字符串执行Base64.decode时,将以以下方式"gid://api/User/150223/Ticket/1"输出。然后,您可以在此输出上构建一些逻辑,从而为特定票证ID的所有用户计数。