class Phoenix::Socket

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.connect

The 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.cr

Constant 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

Instance Method Summary

Constructor Detail

def self.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

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

[View source]
def self.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

socket = Phoenix::Socket.new(
  host: "example.com", path: "/socket", port: 80, tls: false
)

Optional keyword arguments may be provided as above.


[View source]

Instance Method Detail

def channel(topic : String, params = {} of String => JSON::Any) : Channel #

Initiates a new channel for the given topic

channel = socket.channel("topic:subtopic")

[View source]
def connect #

Initiates the WebSocket and spawns a connection fiber


[View source]
def connected? : Bool #

Whether the socket is connected or not


[View source]
def disconnect(callback : -> ? = nil, reason : String? = nil) #

[View source]
def on_close(&block : String -> ) #

Registers callbacks for connection close events

socket.on_close do |raw_msg|
  puts "close callback: #{raw_msg}"
end

[View source]
def on_error(&block : String -> ) #

Registers callbacks for connection error events

socket.on_error do |raw_msg|
  puts "error callback: #{raw_msg}"
end

[View source]
def on_message(&block : String -> ) #

Registers callbacks for connection message events

socket.on_message do |raw_msg|
  puts "message callback: #{raw_msg}"
end

[View source]
def on_open(&block : -> ) #

Registers callbacks for connection open events

socket.on_open do
  puts "open callback"
end

[View source]
def remove(channel : Channel) #

Removes a previously initiated channel


[View source]