Class: Rage::Cable::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/rage/cable/connection.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

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

Note:

This method is only available when Rage::Cable::RemoteDisconnect is included in your connection class.

Disconnect all connections matching the specified identifier.

Only one identifier can be provided per call. To disconnect connections matching multiple criteria, define a composite identifier.

Examples:

Disconnect all connections for a user

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

Using a composite identifier

class RageCable::Connection < Rage::Cable::Connection
  include Rage::Cable::RemoteDisconnect

  identified_by :current_user, :current_user_device

  def connect
    self.current_user = find_user
    self.current_user_device = [current_user, request.headers["Device-Id"]]
  end
end

# Disconnect only the user's specific device
RageCable::Connection.disconnect(current_user_device: [user, "device-123"])

Using protocol options

RageCable::Connection.disconnect(current_user: User.find(1), protocol_options: { reconnect: false })

Parameters:

  • identified_by (Hash) —

    a single key-value pair where the key is a declared identifier

  • protocol_options (Hash) (defaults to: nil) —

    options passed to the connection's protocol when disconnecting

Raises:

  • (ArgumentError) —

    if no identifier, multiple identifiers, or an unknown identifier is provided



# File 'lib/rage/cable/connection.rb', line 110

.identified_by(*identifiers) ⇒ Object

Mark a key as being a connection identifier index that can then be used to find the specific connection again later. Common identifiers are current_user and current_account, but could be anything.

Parameters:

  • identifiers (Symbol, Array<Symbol>)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rage/cable/connection.rb', line 16

def self.identified_by(*identifiers)
  identifiers.each do |method_name|
    raise ArgumentError, "Identifier must be a Symbol" unless method_name.is_a?(Symbol)

    (@__identifiers ||= []) << method_name

    define_method(method_name) do
      @__identified_by_map[method_name]
    end

    define_method("#{method_name}=") do |data|
      @__identified_by_map[method_name] = data if data
    end

    Rage::Cable::Channel.__prepare_id_method(method_name)
  end
end

Instance Method Details

#cookies ⇒ Rage::Cookies

Get the cookie object. See Rage::Cookies.

Returns:



67
68
69
# File 'lib/rage/cable/connection.rb', line 67

def cookies
  @__cookies ||= Rage::Cookies.new(@__env, ReadOnlyHash.new)
end

#params ⇒ Hash{Symbol=>String,Array,Hash}

Get URL query parameters.

Returns:

  • (Hash{Symbol=>String,Array,Hash})


81
82
83
# File 'lib/rage/cable/connection.rb', line 81

def params
  @__params ||= Iodine::Rack::Utils.parse_nested_query(@__env["QUERY_STRING"])
end

#reject_unauthorized_connection ⇒ Object

Reject the WebSocket connection.



49
50
51
# File 'lib/rage/cable/connection.rb', line 49

def reject_unauthorized_connection
  @rejected = true
end

#rejected? ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/rage/cable/connection.rb', line 53

def rejected?
  !!@rejected
end

#request ⇒ Rage::Request

Get the request object. See Request.

Returns:



60
61
62
# File 'lib/rage/cable/connection.rb', line 60

def request
  @__request ||= Rage::Request.new(@__env)
end

#session ⇒ Rage::Session

Get the session object. See Session.

Returns:



74
75
76
# File 'lib/rage/cable/connection.rb', line 74

def session
  @__session ||= Rage::Session.new(cookies)
end