Module: ActionCable::Channel::Broadcasting::ClassMethods

Defined in:
actioncable/lib/action_cable/channel/broadcasting.rb

Instance Method Summary collapse

Instance Method Details

#broadcast_to(model, message) ⇒ Object

Broadcast a hash to a unique broadcasting for this model in this channel.



16
17
18
# File 'actioncable/lib/action_cable/channel/broadcasting.rb', line 16

def broadcast_to(model, message)
  ActionCable.server.broadcast(broadcasting_for(model), message)
end

#broadcasting_for(model) ⇒ Object

Returns a unique broadcasting identifier for this model in this channel:

CommentsChannel.broadcasting_for("all") # => "comments:all"

You can pass any object as a target (e.g. Active Record model), and it would be serialized into a string under the hood.



26
27
28
# File 'actioncable/lib/action_cable/channel/broadcasting.rb', line 26

def broadcasting_for(model)
  serialize_broadcasting([ channel_name, model ])
end

#serialize_broadcasting(object) ⇒ Object

:nodoc:



30
31
32
33
34
35
36
37
38
39
# File 'actioncable/lib/action_cable/channel/broadcasting.rb', line 30

def serialize_broadcasting(object) # :nodoc:
  case
  when object.is_a?(Array)
    object.map { |m| serialize_broadcasting(m) }.join(":")
  when object.respond_to?(:to_gid_param)
    object.to_gid_param
  else
    object.to_param
  end
end