abstract class
Amber::WebSockets::Channel
- Amber::WebSockets::Channel
- Reference
- Object
Overview
Sockets subscribe to Channels, where the communication log is handled. The channel provides functionality
to handle socket join #handle_joined and socket messages handle_message(msg).
Example:
class ChatChannel < Amber::WebSockets::Channel
def handle_joined(client_socket, message)
# functionality when the user joins the channel, optional
end
def handle_leave(client_socket)
# functionality when the user leaves the channel, optional
end
def after_join(client_socket)
# called after handle_joined completes, optional
end
def after_leave(client_socket)
# called after handle_leave completes, optional
end
def on_error(ex, client_socket)
# called when an error occurs during message handling, optional
end
# functionality when a socket sends a message to a channel, required
def handle_message(client_socket, msg)
rebroadcast!(msg)
end
end
Defined in:
amber/websockets/channel.crConstant Summary
-
Log =
::Log.for(self)
Constructors
Class Method Summary
-
.broadcast_to(channel_topic : String, event : String, payload : Hash(String, String))
Broadcasts a message to all subscribers of the given topic from outside a channel instance.
-
.on_message(topic_path : String, client_socket_id : String, message : JSON::Any)
Called from proc when message is returned from the pubsub service This is a class method that handles message dispatch to instances
-
.presence_list(topic_path : String) : Hash(String, Hash(String, String))
Class-level access to presence data for a given topic.
-
.reset_presence
Resets presence tracking.
Instance Method Summary
-
#after_join(client_socket)
Called after
#handle_joinedcompletes successfully. -
#after_leave(client_socket)
Called after
#handle_leavecompletes successfully. -
#broadcast!(message, topic = @topic_path)
Sends message to all subscribing clients belonging to this channel by using the rebroadcast functionality that sends to all subscribers
-
#handle_joined(client_socket, message)
Authorization can happen here
- #handle_leave(client_socket)
- #handle_message(client_socket, msg)
-
#on_error(ex : Exception, client_socket)
Called when an error occurs during message handling or channel callbacks.
-
#presence_count : Int32
Returns the number of sockets currently present in this channel's topic.
-
#presence_list : Hash(String, Hash(String, String))
Returns the list of currently present sockets in this channel's topic.
- #rebroadcast!(message, topic = @topic_path)
-
#subscribe_to_channel(client_socket, message)
Called when a socket subscribes to a channel
-
#unsubscribe_from_channel(client_socket)
Called when a socket unsubscribes from a channel
Constructor Detail
Class Method Detail
Broadcasts a message to all subscribers of the given topic from outside a channel instance. This is useful for sending messages from controllers, background jobs, or other non-channel contexts.
Example:
# From a controller action:
ChatChannel.broadcast_to("chat_room:lobby", "msg:new", {"message" => "Server announcement"})
Called from proc when message is returned from the pubsub service This is a class method that handles message dispatch to instances
Class-level access to presence data for a given topic.
Instance Method Detail
Called after #handle_joined completes successfully.
Override this to perform post-join logic such as sending welcome messages
or notifying other users.
Called after #handle_leave completes successfully.
Override this to perform cleanup logic after a user leaves a channel.
Sends message to all subscribing clients belonging to this channel by using the rebroadcast functionality that sends to all subscribers
Called when an error occurs during message handling or channel callbacks. Override this to implement custom error reporting or recovery logic.
The default implementation logs the error.
Returns the number of sockets currently present in this channel's topic.
Returns the list of currently present sockets in this channel's topic.
Each entry is a Hash with socket_id as key and metadata as value. Metadata includes at minimum "socket_id" and "joined_at".