abstract struct
Amber::WebSockets::ClientSocket
- Amber::WebSockets::ClientSocket
- Struct
- Value
- Object
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.crConstant 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
-
.new(socket : HTTP::WebSocket, context : HTTP::Server::Context, connection_id : String)
Initialize with an existing connection_id for reconnection.
- .new(socket : HTTP::WebSocket, context : HTTP::Server::Context)
Class Method Summary
-
.broadcast(event : String, topic : String, subject : String, payload : Hash)
Broadcast a message to all subscribers of the topic
-
.channel(channel_path, channel_class)
Add a channel class for this socket type to register
- .channels
-
.decoder : Decoders::Decoder
Returns the decoder instance for this socket type.
- .get_topic_channel(topic_path)
Instance Method Summary
-
#channels : Hash(String, Amber::WebSockets::Channel)
Each socket instance has its own channels (instances created from registered classes)
-
#channels=(channels : Hash(String, Amber::WebSockets::Channel))
Each socket instance has its own channels (instances created from registered classes)
-
#connection_id : String
A stable identifier that persists across reconnections.
-
#get_channel(path : String) : Channel | Nil
Helper method to get a channel instance for this socket
-
#handle_error(ex : Exception, context : String = "unknown")
Override to implement custom error handling logic.
-
#on_connect : Bool
Authentication and authorization can happen here
-
#on_disconnect
On socket disconnect functionality
-
#on_error(ex : Exception)
Called when an error occurs at the socket level (outside of a channel).
-
#on_reconnect
Called when a previously disconnected socket reconnects within the reconnection window.
- #socket : HTTP::WebSocket
Constructor Detail
Initialize with an existing connection_id for reconnection.
Class Method Detail
Broadcast a message to all subscribers of the topic
UserSocket.broadcast("message", "chats_room:1", "msg:new", {"message" => "test"})
Add a channel class for this socket type to register
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
Instance Method Detail
Each socket instance has its own channels (instances created from registered classes)
Each socket instance has its own channels (instances created from registered classes)
A stable identifier that persists across reconnections. When a client reconnects, it can present its connection_id to resume a previous session.
Helper method to get a channel instance for this socket
Override to implement custom error handling logic. This is a hook that allows subclasses to report errors to external services.
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.
Called when a previously disconnected socket reconnects within the reconnection window. Override to restore channel state, send missed data, or notify other users.