Class: ActionCable::Connection::Base
- Inherits:
-
Object
- Object
- ActionCable::Connection::Base
- Includes:
- Authorization, Identification, InternalChannel
- Defined in:
- lib/action_cable/connection/base.rb
Overview
For every WebSocket connection the Action Cable server accepts, a Connection object will be instantiated. This instance becomes the parent of all of the channel subscriptions that are created from there on. Incoming messages are then routed to these channel subscriptions based on an identifier sent by the Action Cable consumer. The Connection itself does not deal with any specific application logic beyond authentication and authorization.
Here’s a basic example:
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
logger. current_user.name
end
def disconnect
# Any cleanup work needed when the cable connection is cut.
end
private
def find_verified_user
User.find_by_identity(.signed[:identity_id]) ||
end
end
end
First, we declare that this connection can be identified by its current_user. This allows us to later be able to find all connections established for that current_user (and potentially disconnect them). You can declare as many identification indexes as you like. Declaring an identification means that an attr_accessor is automatically set for that key.
Second, we rely on the fact that the WebSocket connection is established with the cookies from the domain being sent along. This makes it easy to use signed cookies that were set when logging in via a web interface to authorize the WebSocket connection.
Finally, we add a tag to the connection-specific logger with the name of the current user to easily distinguish their messages in the log.
Pretty simple, eh?
Direct Known Subclasses
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#subscriptions ⇒ Object
readonly
Returns the value of attribute subscriptions.
-
#worker_pool ⇒ Object
readonly
Returns the value of attribute worker_pool.
Instance Method Summary collapse
- #beat ⇒ Object
-
#close ⇒ Object
Close the WebSocket connection.
-
#dispatch_websocket_message(websocket_message) ⇒ Object
:nodoc:.
-
#initialize(server, env, coder: ActiveSupport::JSON) ⇒ Base
constructor
A new instance of Base.
-
#on_close(reason, code) ⇒ Object
:nodoc:.
-
#on_error(message) ⇒ Object
:nodoc:.
-
#on_message(message) ⇒ Object
:nodoc:.
-
#on_open ⇒ Object
:nodoc:.
-
#process ⇒ Object
Called by the server when a new WebSocket connection is established.
-
#receive(websocket_message) ⇒ Object
Decodes WebSocket messages and dispatches them to subscribed channels.
-
#send_async(method, *arguments) ⇒ Object
Invoke a method on the connection asynchronously through the pool of thread workers.
-
#statistics ⇒ Object
Return a basic hash of statistics for the connection keyed with
identifier
,started_at
,subscriptions
, andrequest_id
. -
#transmit(cable_message) ⇒ Object
:nodoc:.
Methods included from Authorization
#reject_unauthorized_connection
Methods included from Identification
Constructor Details
#initialize(server, env, coder: ActiveSupport::JSON) ⇒ Base
Returns a new instance of Base.
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/action_cable/connection/base.rb', line 51 def initialize(server, env, coder: ActiveSupport::JSON) @server, @env, @coder = server, env, coder @worker_pool = server.worker_pool @logger = new_tagged_logger @websocket = ActionCable::Connection::WebSocket.new(env, self, event_loop) @subscriptions = ActionCable::Connection::Subscriptions.new(self) @message_buffer = ActionCable::Connection::MessageBuffer.new(self) @_internal_subscriptions = nil @started_at = Time.now end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
48 49 50 |
# File 'lib/action_cable/connection/base.rb', line 48 def env @env end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
48 49 50 |
# File 'lib/action_cable/connection/base.rb', line 48 def logger @logger end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
48 49 50 |
# File 'lib/action_cable/connection/base.rb', line 48 def protocol @protocol end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
48 49 50 |
# File 'lib/action_cable/connection/base.rb', line 48 def server @server end |
#subscriptions ⇒ Object (readonly)
Returns the value of attribute subscriptions.
48 49 50 |
# File 'lib/action_cable/connection/base.rb', line 48 def subscriptions @subscriptions end |
#worker_pool ⇒ Object (readonly)
Returns the value of attribute worker_pool.
48 49 50 |
# File 'lib/action_cable/connection/base.rb', line 48 def worker_pool @worker_pool end |
Instance Method Details
#beat ⇒ Object
116 117 118 |
# File 'lib/action_cable/connection/base.rb', line 116 def beat transmit type: ActionCable::INTERNAL[:message_types][:ping], message: Time.now.to_i end |
#close ⇒ Object
Close the WebSocket connection.
96 97 98 |
# File 'lib/action_cable/connection/base.rb', line 96 def close websocket.close end |
#dispatch_websocket_message(websocket_message) ⇒ Object
:nodoc:
83 84 85 86 87 88 89 |
# File 'lib/action_cable/connection/base.rb', line 83 def () #:nodoc: if websocket.alive? subscriptions.execute_command decode() else logger.error "Ignoring message processed after the WebSocket was closed: #{.inspect})" end end |
#on_close(reason, code) ⇒ Object
:nodoc:
133 134 135 |
# File 'lib/action_cable/connection/base.rb', line 133 def on_close(reason, code) # :nodoc: send_async :handle_close end |
#on_error(message) ⇒ Object
:nodoc:
128 129 130 131 |
# File 'lib/action_cable/connection/base.rb', line 128 def on_error() # :nodoc: # log errors to make diagnosing socket errors easier logger.error "WebSocket error occurred: #{}" end |
#on_message(message) ⇒ Object
:nodoc:
124 125 126 |
# File 'lib/action_cable/connection/base.rb', line 124 def () # :nodoc: .append end |
#on_open ⇒ Object
:nodoc:
120 121 122 |
# File 'lib/action_cable/connection/base.rb', line 120 def on_open # :nodoc: send_async :handle_open end |
#process ⇒ Object
Called by the server when a new WebSocket connection is established. This configures the callbacks intended for overwriting by the user. This method should not be called directly – instead rely upon on the #connect (and #disconnect) callbacks.
67 68 69 70 71 72 73 74 75 |
# File 'lib/action_cable/connection/base.rb', line 67 def process #:nodoc: logger.info if websocket.possible? && allow_request_origin? respond_to_successful_request else respond_to_invalid_request end end |
#receive(websocket_message) ⇒ Object
Decodes WebSocket messages and dispatches them to subscribed channels. WebSocket message transfer encoding is always JSON.
79 80 81 |
# File 'lib/action_cable/connection/base.rb', line 79 def receive() #:nodoc: send_async :dispatch_websocket_message, end |
#send_async(method, *arguments) ⇒ Object
Invoke a method on the connection asynchronously through the pool of thread workers.
101 102 103 |
# File 'lib/action_cable/connection/base.rb', line 101 def send_async(method, *arguments) worker_pool.async_invoke(self, method, *arguments) end |
#statistics ⇒ Object
Return a basic hash of statistics for the connection keyed with identifier
, started_at
, subscriptions
, and request_id
. This can be returned by a health check against the connection.
107 108 109 110 111 112 113 114 |
# File 'lib/action_cable/connection/base.rb', line 107 def statistics { identifier: connection_identifier, started_at: @started_at, subscriptions: subscriptions.identifiers, request_id: @env["action_dispatch.request_id"] } end |
#transmit(cable_message) ⇒ Object
:nodoc:
91 92 93 |
# File 'lib/action_cable/connection/base.rb', line 91 def transmit() # :nodoc: websocket.transmit encode() end |