Module: Rage::Cable::RemoteDisconnect::ClassMethods

Defined in:
lib/rage/cable/remote_disconnect.rb

Instance Method Summary collapse

Instance Method Details

#disconnect(protocol_options: nil, **identified_by) ⇒ Object

Disconnect all connections matching the specified identifier.

Examples:

Disconnect all connections for a user

RageCable::Connection.disconnect(current_user: User.find(1))

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rage/cable/remote_disconnect.rb', line 32

def disconnect(protocol_options: nil, **identified_by)
  raise ArgumentError, "An identifier must be provided" if identified_by.empty?
  raise ArgumentError, "Only one identifier can be provided" if identified_by.size > 1

  id_pair = identified_by.first
  unless __identifiers&.include?(id_pair[0])
    raise ArgumentError, "Unknown identifier: #{id_pair[0]}"
  end

  message = if protocol_options
    raise ArgumentError, "protocol_options must be a Hash" unless protocol_options.is_a?(Hash)
    protocol_options.to_json
  else
    ""
  end

  Rage::PubSub.publish(message, topic: ["rage", name, id_pair])
end