2017-07-16 13:10:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-06-28 14:24:50 -04:00
|
|
|
module ActionCable
|
|
|
|
module Server
|
2016-02-13 21:05:04 -05:00
|
|
|
# Broadcasting is how other parts of your application can send messages to a channel's subscribers. As explained in Channel, most of the time, these
|
2015-07-07 17:13:00 -04:00
|
|
|
# broadcastings are streamed directly to the clients subscribed to the named broadcasting. Let's explain with a full-stack example:
|
|
|
|
#
|
|
|
|
# class WebNotificationsChannel < ApplicationCable::Channel
|
2016-02-02 11:16:41 -05:00
|
|
|
# def subscribed
|
|
|
|
# stream_from "web_notifications_#{current_user.id}"
|
|
|
|
# end
|
|
|
|
# end
|
2015-07-07 17:13:00 -04:00
|
|
|
#
|
2016-02-13 21:05:04 -05:00
|
|
|
# # Somewhere in your app this is called, perhaps from a NewCommentJob:
|
2016-02-02 11:16:41 -05:00
|
|
|
# ActionCable.server.broadcast \
|
|
|
|
# "web_notifications_1", { title: "New things!", body: "All that's fit for print" }
|
2015-07-07 17:13:00 -04:00
|
|
|
#
|
2016-02-13 21:05:04 -05:00
|
|
|
# # Client-side CoffeeScript, which assumes you've already requested the right to send web notifications:
|
2016-02-02 11:16:41 -05:00
|
|
|
# App.cable.subscriptions.create "WebNotificationsChannel",
|
|
|
|
# received: (data) ->
|
|
|
|
# new Notification data['title'], body: data['body']
|
2015-06-28 14:24:50 -04:00
|
|
|
module Broadcasting
|
2016-02-13 21:05:04 -05:00
|
|
|
# Broadcast a hash directly to a named <tt>broadcasting</tt>. This will later be JSON encoded.
|
2016-03-11 18:32:02 -05:00
|
|
|
def broadcast(broadcasting, message, coder: ActiveSupport::JSON)
|
|
|
|
broadcaster_for(broadcasting, coder: coder).broadcast(message)
|
2015-06-28 14:36:10 -04:00
|
|
|
end
|
|
|
|
|
2016-02-13 06:36:16 -05:00
|
|
|
# Returns a broadcaster for a named <tt>broadcasting</tt> that can be reused. Useful when you have an object that
|
2015-07-07 17:13:00 -04:00
|
|
|
# may need multiple spots to transmit to a specific broadcasting over and over.
|
2016-03-11 18:32:02 -05:00
|
|
|
def broadcaster_for(broadcasting, coder: ActiveSupport::JSON)
|
|
|
|
Broadcaster.new(self, String(broadcasting), coder: coder)
|
2015-06-28 14:24:50 -04:00
|
|
|
end
|
|
|
|
|
2015-06-28 14:42:49 -04:00
|
|
|
private
|
2015-06-28 14:36:10 -04:00
|
|
|
class Broadcaster
|
2016-03-11 18:32:02 -05:00
|
|
|
attr_reader :server, :broadcasting, :coder
|
2015-06-28 14:42:49 -04:00
|
|
|
|
2016-03-11 18:32:02 -05:00
|
|
|
def initialize(server, broadcasting, coder:)
|
|
|
|
@server, @broadcasting, @coder = server, broadcasting, coder
|
2015-06-28 14:36:10 -04:00
|
|
|
end
|
2015-06-28 14:24:50 -04:00
|
|
|
|
2015-06-28 14:38:05 -04:00
|
|
|
def broadcast(message)
|
2021-07-07 13:47:21 -04:00
|
|
|
server.logger.debug { "[ActionCable] Broadcasting to #{broadcasting}: #{message.inspect.truncate(300)}" }
|
2016-05-11 19:55:17 -04:00
|
|
|
|
|
|
|
payload = { broadcasting: broadcasting, message: message, coder: coder }
|
|
|
|
ActiveSupport::Notifications.instrument("broadcast.action_cable", payload) do
|
|
|
|
encoded = coder ? coder.encode(message) : message
|
|
|
|
server.pubsub.broadcast broadcasting, encoded
|
|
|
|
end
|
2015-06-28 14:36:10 -04:00
|
|
|
end
|
2015-06-28 14:24:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-10-16 04:02:22 -04:00
|
|
|
end
|