abstract struct Amber::WebSockets::ClientSocket

Overview

ClientSocket struct maps a user to an HTTP::WebSocket. For every websocket connection there will be an associated ClientSocket. Authentication and authorization happen within the ClientSocket. ClientSocket will subscribe to Channels, where incoming and outgoing messages are routed through.

Example:

struct UserSocket < Amber::WebSockets::ClientSocket
  channel "user_channel:*", UserChannel
  channel "room_channel:*", RoomChannel

  # Optional: override the default decoder
  def self.decoder
    Amber::WebSockets::Decoders::TextDecoder.new
  end

  def on_connect
    return some_auth_method!
  end
end

Direct Known Subclasses

Defined in:

amber/websockets/client_socket.cr

Constant Summary

BEAT_INTERVAL = 30.seconds
DEFAULT_MESSAGE_BUFFER_SIZE = 100

Default maximum number of messages to buffer during a disconnection.

Log = ::Log.for(self)
MAX_SOCKET_IDLE_TIME = 100.seconds
RECONNECT_WINDOW = 60.seconds

Default reconnection window: how long a disconnected socket can reconnect and recover buffered messages.

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(socket : HTTP::WebSocket, context : HTTP::Server::Context, connection_id : String) #

Initialize with an existing connection_id for reconnection.


[View source]
def self.new(socket : HTTP::WebSocket, context : HTTP::Server::Context) #

[View source]

Class Method Detail

def self.broadcast(event : String, topic : String, subject : String, payload : Hash) #

Broadcast a message to all subscribers of the topic

UserSocket.broadcast("message", "chats_room:1", "msg:new", {"message" => "test"})

[View source]
def self.channel(channel_path, channel_class) #

Add a channel class for this socket type to register


[View source]
def self.channels #

[View source]
def self.decoder : Decoders::Decoder #

Returns the decoder instance for this socket type. Override in subclasses to use a different decoder.

Example:

struct BinarySocket < Amber::WebSockets::ClientSocket
  def self.decoder
    Amber::WebSockets::Decoders::BinaryDecoder.new
  end
end

[View source]
def self.get_topic_channel(topic_path) #

[View source]

Instance Method Detail

def channels : Hash(String, Amber::WebSockets::Channel) #

Each socket instance has its own channels (instances created from registered classes)


[View source]
def channels=(channels : Hash(String, Amber::WebSockets::Channel)) #

Each socket instance has its own channels (instances created from registered classes)


[View source]
def connection_id : String #

A stable identifier that persists across reconnections. When a client reconnects, it can present its connection_id to resume a previous session.


[View source]
def get_channel(path : String) : Channel | Nil #

Helper method to get a channel instance for this socket


[View source]
def handle_error(ex : Exception, context : String = "unknown") #

Override to implement custom error handling logic. This is a hook that allows subclasses to report errors to external services.


[View source]
def on_connect : Bool #

Authentication and authorization can happen here


[View source]
def on_disconnect #

On socket disconnect functionality


[View source]
def on_error(ex : Exception) #

Called when an error occurs at the socket level (outside of a channel). Override to implement custom error reporting.

The default implementation logs the error.


[View source]
def on_reconnect #

Called when a previously disconnected socket reconnects within the reconnection window. Override to restore channel state, send missed data, or notify other users.


[View source]
def socket : HTTP::WebSocket #

[View source]