ruby on rails - How can I cleanly broadcast to multiple channels at once with ActionCable? -
i have code:
actioncable.server.broadcast "posts_#{self.post_id}", { object: some_object } i'd send same payload multiple broadcastings. obvious way pass array of broadcastings:
actioncable.server.broadcast ["posts_#{self.post_id}", "all_posts"], \ { object: some_object } but seems treat entire array being identifier:
[actioncable] broadcasting ["posts_30453", "all_posts"]: ... and neither broadcasting recieves message.
it seems there should cleaner way calling actioncable.server.broadcast multiple times. there?
i build tiny helper gem (multicable) this. adds broadcast_multiple method actioncable::server:
> actioncable.server.broadcast_multiple ["broadcasting1", "broadcasting2"], { payload: "whatever" } [actioncable] broadcasting broadcasting1: {:payload=>"whatever"} [actioncable] broadcasting broadcasting2: {:payload=>"whatever"} either install gem (gem 'multicable') or throw tiny source somewhere in project:
actioncable::server::base.class_eval def broadcast_multiple(broadcastings, message, coder: activesupport::json) broadcastings.each |broadcasting| broadcast(broadcasting, message, coder: coder) end end end
Comments
Post a Comment