Skip to Content Skip to Search

This class encapsulates all the low-level logic of working with the underlying WebSocket conenctions and delegate all the business-logic to the user-level connection object (e.g., ApplicationCable::Connection). This connection object is also responsible for handling encoding and decoding of messages, so the user-level connection object shouldn’t know about such details.

Methods
C
N
P
R
S
T

Attributes

[R] connection
[R] env
[R] logger
[R] protocol
[R] server

Class Public methods

new(server, env, coder: ActiveSupport::JSON)

# File actioncable/lib/action_cable/server/socket.rb, line 18
def initialize(server, env, coder: ActiveSupport::JSON)
  @server, @env, @coder = server, env, coder

  @worker_pool = server.worker_pool
  @logger = server.new_tagged_logger { request }

  @websocket      = WebSocket.new(env, self, event_loop)
  @message_buffer = MessageBuffer.new(self)

  @protocol = nil
  @connection = config.connection_class.call.new(server, self)
end

Instance Public methods

close(...)

Close the WebSocket connection.

# File actioncable/lib/action_cable/server/socket.rb, line 52
def close(...)
  websocket.close(...) if websocket.alive?
end

perform_work(receiver, method, *args)

Invoke a method on the connection asynchronously through the pool of thread workers.

# File actioncable/lib/action_cable/server/socket.rb, line 57
def perform_work(receiver, method, *args)
  worker_pool.async_invoke(receiver, method, *args, connection: self)
end

request()

The request that initiated the WebSocket connection is available here. This gives access to the environment, cookies, etc.

# File actioncable/lib/action_cable/server/socket.rb, line 66
def request
  @request ||= begin
    environment = Rails.application.env_config.merge(env) if defined?(Rails.application) && Rails.application
    ActionDispatch::Request.new(environment || env)
  end
end

send_async(method, *arguments)

# File actioncable/lib/action_cable/server/socket.rb, line 61
def send_async(method, *arguments)
  worker_pool.async_invoke(self, method, *arguments)
end

transmit(cable_message)

Send a non-serialized message over the WebSocket connection.

# File actioncable/lib/action_cable/server/socket.rb, line 45
def transmit(cable_message)
  return unless websocket.alive?

  websocket.transmit encode(cable_message)
end