Ruby on rails rails中actioncable的默认行为是什么?

Ruby on rails rails中actioncable的默认行为是什么?,ruby-on-rails,ruby,actioncable,Ruby On Rails,Ruby,Actioncable,生成新通道时,方法调用中有注释掉的流_。我知道,它适合于识别流,比如来自“comments_35;{message.id}的流 但是,如果这个频道没有这样的目标,应该流式处理所有评论?如果不指定stream_from,此频道的默认行为(可能是值)是什么?假设您的频道名为SomethingChannel class SomethingChannel < ApplicationCable::Channel def subscribed # because you do not ne

生成新通道时,方法调用中有注释掉的流_。我知道,它适合于识别流,比如来自“comments_35;{message.id}的流


但是,如果这个频道没有这样的目标,应该流式处理所有评论?如果不指定stream_from,此频道的默认行为(可能是值)是什么?

假设您的频道名为
SomethingChannel

class SomethingChannel < ApplicationCable::Channel
  def subscribed
    # because you do not need stream_from, then remove the stream_from below
    # stream_from "something_channel"

    # and just immediately transmit the data. This should be a hash, and thus I use `as_json`; Change this accordingly as you see fit
    transmit(Comment.all.as_json)
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end
end
您应该在Google Chrome/Firefox控制台中看到如下内容:

connected
received
▼ [{…}]
  ▶ 0: {id: 1, title: "Hello ", content: "World from Earth! :)", created_at: "2018-02-13T16:15:05.734Z", updated_at: "2018-02-13T16:15:05.734Z"}
  ▶ 1: {id: 2, title: "Lorem ", content: "Ipsum Dolor", created_at: "2018-02-13T16:15:05.734Z", updated_at: "2018-02-13T16:15:05.734Z"}
  length: 2
  ▶ __proto__: Array(0)

另外,如果你不打算使用
stream\u from
stream\u for
,那么你可能根本不需要
ActionCable
,也许你最好从API检索所有注释(即
GET/comments

我不明白。我只是使用ActionCable.server.broadcast向订阅者广播一些数据。但是,如果我没有设置stream_,我应该传递给ActionCable.server.broadcast的第一个参数是什么?在教程中,ActionCable.server.broadcast第一个参数和stream_from参数相等。但是默认设置是什么?如果要执行
ActionCable.server.broadcast
,则需要使用
stream\u for
stream\u from
。主要原因是可伸缩性。您需要有一个供所有订阅者收听的中心位置,即“广播名称”(与您传递给(广播名称)流的值相同)。此“广播名称”位于一个中央分发位置,通常是您的
redis
configure for
ActionCable
。。。。不使用“广播名称”的问题在于,您的应用程序可能有很多计算机/服务器,每个计算机/服务器都运行自己的应用程序Rails服务器(以便能够处理大量并发请求),并且您没有使用中心位置“广播名称”/redis),然后
ActionCable.server.broadcast
只能广播到当前连接到它的所有Websocket连接,但只能广播到一台服务器,而不能广播到您的所有服务器,因为每台服务器都有自己的“内存”空间,并且它们不能自动访问彼此的“内存”如果您坚持不想使用
stream\u from
,但同时希望能够“广播”(如
ActionCable.server.broadcast
),则可以执行以下操作:
ObjectSpace.each\u对象(SomethingChannel){| something_channel_instance | something_channel_instance.connection.transmit(YOURDATA)}
但是这只能在同一台服务器上工作(多进程或多服务器都不能工作),而且这并不是一种推荐的方法,从长远来看,您可能会遇到维护问题。此外,我还没有完全测试这是否有效。
connected
received
▼ [{…}]
  ▶ 0: {id: 1, title: "Hello ", content: "World from Earth! :)", created_at: "2018-02-13T16:15:05.734Z", updated_at: "2018-02-13T16:15:05.734Z"}
  ▶ 1: {id: 2, title: "Lorem ", content: "Ipsum Dolor", created_at: "2018-02-13T16:15:05.734Z", updated_at: "2018-02-13T16:15:05.734Z"}
  length: 2
  ▶ __proto__: Array(0)