class Phoenix::Socket
- Phoenix::Socket
- Reference
- Object
Overview
A single connection is established to the server and channels are multiplexed over the connection. Connect to the server using the Socket class:
socket = Phoenix::Socket.new(
  "http://example.com/socket",
  params: {"userToken" => "123"}
)
socket.connectThe Socket constructor takes the endpoint of the socket, the authentication params, as well as options that can be found below, such as configuring the logger, and heartbeat.
Defined in:
phoenix/socket.crConstant Summary
- 
        DEFAULT_HEARTBEAT_INTERVAL_MS = 30000_u32
- 
        Default heartbeat interval in milliseconds 
- 
        DEFAULT_RECONNECT_AFTER_MS = Proc(UInt32, UInt32).new do |tries| [1000_u32, 2000_u32, 5000_u32, 10000_u32].fetch(tries - 1) do 10000_u32 end end
- 
        Default reconnection timeout implements stepped backoff 
- 
        DEFAULT_TIMEOUT = 10000_u32
- 
        Default timeout in milliseconds to trigger push timeouts 
- 
        VSN = "2.0.0"
- 
        Wire protocol version 
Constructors
- 
        .new(endpoint : URI | String, headers : HTTP::Headers = HTTP::Headers.new, timeout : UInt32 = DEFAULT_TIMEOUT, encode : Message -> String = ->(msg : Message) do
  Serializer.encode(msg)
end, decode : String -> Message = ->(raw_msg : String) do
  Serializer.decode(raw_msg)
end, heartbeat_interval_ms : UInt32 = DEFAULT_HEARTBEAT_INTERVAL_MS, reconnect_after_ms : UInt32 -> UInt32 = DEFAULT_RECONNECT_AFTER_MS, logger : String, String, JSON::Any -> ? = nil, params = {} of String => String)
        
          Create a socket with a provided endpoint URI or string 
- 
        .new(host : String = "localhost", path : String = "/socket", port : Int32? = 4000, tls : Bool = false, headers : HTTP::Headers = HTTP::Headers.new, timeout : UInt32 = DEFAULT_TIMEOUT, encode : Message -> String = ->(msg : Message) do
  Serializer.encode(msg)
end, decode : String -> Message = ->(raw_msg : String) do
  Serializer.decode(raw_msg)
end, heartbeat_interval_ms : UInt32 = DEFAULT_HEARTBEAT_INTERVAL_MS, reconnect_after_ms : UInt32 -> UInt32 = DEFAULT_RECONNECT_AFTER_MS, logger : String, String, JSON::Any -> ? = nil, params = {} of String => String)
        
          Create a socket with a provided host, path, port and tls state 
Instance Method Summary
- 
        #channel(topic : String, params = {} of String => JSON::Any) : Channel
        
          Initiates a new channel for the given topic 
- 
        #connect
        
          Initiates the WebSocket and spawns a connection fiber 
- 
        #connected? : Bool
        
          Whether the socket is connected or not 
- #disconnect(callback : -> ? = nil, reason : String? = nil)
- 
        #on_close(&block : String -> )
        
          Registers callbacks for connection close events 
- 
        #on_error(&block : String -> )
        
          Registers callbacks for connection error events 
- 
        #on_message(&block : String -> )
        
          Registers callbacks for connection message events 
- 
        #on_open(&block :  -> )
        
          Registers callbacks for connection open events 
- 
        #remove(channel : Channel)
        
          Removes a previously initiated channel 
Constructor Detail
Create a socket with a provided endpoint URI or string
socket = Phoenix::Socket.new("http://example.com/socket")Optionally provide keyword arguments for the following:
- headers: connection headers
- timeout: timeout in milliseconds to trigger push timeouts
- encode: proc to encode outgoing messages
- decode: proc to decode incoming messages
- heartbeat_interval_ms: millisecond interval to send a heartbeat message
- reconnect_after_ms: proc that returns the millisecond reconnect interval
- logger: proc for specialized logging
- params: params to pass when connecting
Create a socket with a provided host, path, port and tls state
socket = Phoenix::Socket.new(
  host: "example.com", path: "/socket", port: 80, tls: false
)Optional keyword arguments may be provided as above.
Instance Method Detail
Initiates a new channel for the given topic
channel = socket.channel("topic:subtopic")Registers callbacks for connection close events
socket.on_close do |raw_msg|
  puts "close callback: #{raw_msg}"
endRegisters callbacks for connection error events
socket.on_error do |raw_msg|
  puts "error callback: #{raw_msg}"
endRegisters callbacks for connection message events
socket.on_message do |raw_msg|
  puts "message callback: #{raw_msg}"
endRegisters callbacks for connection open events
socket.on_open do
  puts "open callback"
end