class Base
Ruby on Rails 7.2.3
Since v5.2.8.1Action Cable Connection Base
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.add_tags 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(cookies.encrypted[:identity_id]) || reject_unauthorized_connection 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?
Inherits from
Includes
Attributes
- ActionCable::Connection::Base#env
- ActionCable::Connection::Base#logger
- ActionCable::Connection::Base#message_buffer
- ActionCable::Connection::Base#protocol
- ActionCable::Connection::Base#server
- ActionCable::Connection::Base#subscriptions
- ActionCable::Connection::Base#websocket
- ActionCable::Connection::Base#worker_pool
Methods (defined here)
- # beat
- # close
- # handle_channel_command
- # send_async
- # statistics
- self. new
Private methods
(15)
Implementation detail — not part of the public API.
- # allow_request_origin?
- # cookies
- # decode
- # encode
- # finished_request_message
- # handle_close
- # handle_open
- # invalid_request_message
- # new_tagged_logger
- # request
- # respond_to_invalid_request
- # respond_to_successful_request
- # send_welcome_message
- # started_request_message
- # successful_request_message
Methods (inherited)
From ActiveSupport::Rescuable (1)
From Object (17)
- # acts_like?
- # blank?
- # deep_dup
- # duplicable?
- # html_safe?
- # in?
- # instance_values
- # instance_variable_names
- # presence
- # presence_in
- # present?
- # to_param
- # to_query
- # try
- # try!
- # with
- # with_options
From ActiveSupport::Callbacks (1)
From ActiveSupport::Concern (3)
- # class_methods
- # included
- # prepended
From ActiveSupport::NumericWithFormat (2)
- # to_formatted_s
- # to_fs
From ActiveSupport::DescendantsTracker (3)
- # descendants
- self. descendants
- self. subclasses