Connection management handles WebSocket authentication, authorization, identification, and provides capabilities for managing connections across multiple servers.
Base class for WebSocket connections providing authentication, authorization, and subscription management.
class ActionCable::Connection::Base
# Declare connection identifiers for finding connections later
# @param identifiers [Array<Symbol>] List of identifier attribute names
def self.identified_by(*identifiers)
end
# Called when WebSocket connection is established
# Override to implement authentication logic
def connect
end
# Called when WebSocket connection is closed
# Override to implement cleanup logic
def disconnect
end
# Reject unauthorized connection attempts
# Closes the WebSocket connection immediately
def reject_unauthorized_connection
end
# Access to the HTTP request environment
attr_reader :env
# Access to the server instance
attr_reader :server
# Access to subscription manager
attr_reader :subscriptions
# Connection logger instance
attr_reader :logger
# WebSocket protocol being used
attr_reader :protocol
endUsage Example:
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user, :current_account
def connect
self.current_user = find_verified_user
self.current_account = current_user.account
logger.add_tags(current_user.email)
end
def disconnect
# Cleanup logic when connection closes
current_user&.update(last_seen_at: Time.current)
end
private
def find_verified_user
# Authenticate using cookies or query parameters
verified_user = User.find_by(id: cookies.encrypted[:user_id])
return verified_user if verified_user
# Reject if authentication fails
reject_unauthorized_connection
end
end
endAuthorization functionality for connections.
module ActionCable::Connection::Authorization
# Reject unauthorized connection
# Closes WebSocket with 'unauthorized' close code
def reject_unauthorized_connection
end
private
# Log rejection reason
def log_unauthorized_connection(reason)
end
endConnection identification system for finding and managing connections.
module ActionCable::Connection::Identification
# Declare identifiers for this connection class
# Creates attr_accessor methods for each identifier
# @param identifiers [Array<Symbol>] Identifier names
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def identified_by(*identifiers)
end
# Returns list of declared identifiers
def identifiers
end
end
# Get connection identifier string for this connection
def connection_identifier
end
endServer-level connection management capabilities.
class ActionCable::Server::Base
# Disconnect connections by identifiers
# @param identifiers [Hash] Hash of identifier key-value pairs
def disconnect(identifiers)
end
# Restart server and close all connections
def restart
end
# Get remote connections manager
# @return [ActionCable::RemoteConnections]
def remote_connections
end
# Get all connection identifiers configured
# @return [Array<Symbol>]
def connection_identifiers
end
endUsage Examples:
# Disconnect specific user across all servers
ActionCable.server.disconnect(current_user: User.find(123))
# Restart server (closes all connections)
ActionCable.server.restart
# Get remote connections manager
remote_conns = ActionCable.server.remote_connections
# Check configured identifiers
identifiers = ActionCable.server.connection_identifiers
# => [:current_user, :current_account]Manage connections across multiple ActionCable servers.
class ActionCable::RemoteConnections
def initialize(server)
end
# Find connections by identifier values
# @param identifier [Hash] Hash of identifier key-value pairs
# @return [ActionCable::RemoteConnections::RemoteConnection]
def where(identifier)
end
attr_reader :server
end
class ActionCable::RemoteConnections::RemoteConnection
def initialize(server, ids)
end
# Disconnect the found connections
# Uses internal channel to broadcast disconnect command
def disconnect
end
# Get all identifiers for this connection type
def identifiers
end
protected
attr_reader :server
endUsage Examples:
# Find and disconnect user across all servers
ActionCable.server.remote_connections
.where(current_user: User.find(123))
.disconnect
# Find and disconnect by multiple identifiers
ActionCable.server.remote_connections
.where(current_user: user, current_account: account)
.disconnectManage channel subscriptions for a connection.
class ActionCable::Connection::Subscriptions
def initialize(connection)
end
# Execute subscription command from client
# @param data [Hash] Subscription command data
def execute_command(data)
end
# Add subscription for identifier
# @param data [Hash] Subscription data with channel and params
def add(data)
end
# Remove subscription by identifier
# @param identifier [String] Subscription identifier
def remove(identifier)
end
# Remove all subscriptions
def unsubscribe_from_all
end
# Find subscription by identifier
def find(identifier)
end
attr_reader :connection
endInternal channel system for server-to-server communication.
module ActionCable::Connection::InternalChannel
# Get the internal channel name for this connection
def internal_channel
end
private
# Subscribe to the internal channel
def subscribe_to_internal_channel
end
# Unsubscribe from the internal channel
def unsubscribe_from_internal_channel
end
# Process internal channel message
def process_internal_message(message)
end
endclass ActionCable::RemoteConnections::RemoteConnection::InvalidIdentifiersError < StandardError
endError Handling Example:
begin
ActionCable.server.remote_connections
.where(invalid_identifier: "value")
.disconnect
rescue ActionCable::RemoteConnections::RemoteConnection::InvalidIdentifiersError
Rails.logger.error "Invalid connection identifiers provided"
endclass ActionCable::Connection::Base
# Check if connection is open
def open?
end
# Get connection state
def state
end
# Close connection with optional reason
def close(reason: nil)
end
endclass ActionCable::Connection::Base
# Process incoming WebSocket message
def receive(websocket_message)
end
# Send message to client
def transmit(data)
end
protected
# Process subscription command
def process_subscription_command(command)
end
end