Module: ActionCable::Server::Broadcasting

Included in:
Base
Defined in:
lib/action_cable/server/broadcasting.rb

Overview

Broadcasting is how other parts of your application can send messages to the channel subscribers. As explained in Channel, most of the time, these broadcastings are streamed directly to the clients subscribed to the named broadcasting. Let's explain with a full-stack example:

class WebNotificationsChannel < ApplicationCable::Channel
   def subscribed
     stream_from "web_notifications_#{current_user.id}"
   end
 end

 # Somewhere in your app this is called, perhaps from a NewCommentJob
 ActionCable.server.broadcast \
   "web_notifications_1", { title: 'New things!', body: 'All shit fit for print' }

 # Client-side coffescript which assumes you've already requested the right to send web notifications
 App.cable.subscriptions.create "WebNotificationsChannel",
   received: (data) ->
     new Notification data['title'], body: data['body']

Defined Under Namespace

Classes: Broadcaster

Instance Method Summary collapse

Instance Method Details

#broadcast(broadcasting, message) ⇒ Object

Broadcast a hash directly to a named broadcasting. It'll automatically be JSON encoded.


22
23
24
# File 'lib/action_cable/server/broadcasting.rb', line 22

def broadcast(broadcasting, message)
  broadcaster_for(broadcasting).broadcast(message)
end

#broadcaster_for(broadcasting) ⇒ Object

Returns a broadcaster for a named broadcasting that can be reused. Useful when you have a object that may need multiple spots to transmit to a specific broadcasting over and over.


28
29
30
# File 'lib/action_cable/server/broadcasting.rb', line 28

def broadcaster_for(broadcasting)
  Broadcaster.new(self, broadcasting)
end

#broadcasting_redisObject

The redis instance used for broadcasting. Not intended for direct user use.


33
34
35
# File 'lib/action_cable/server/broadcasting.rb', line 33

def broadcasting_redis
  @broadcasting_redis ||= Redis.new(config.redis)
end