abstract class Amber::WebSockets::Channel

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

Constant Summary

Log = ::Log.for(self)

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(topic_path : String) #

[View source]

Class Method Detail

def self.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. 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"})

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


[View source]
def self.presence_list(topic_path : String) : Hash(String, Hash(String, String)) #

Class-level access to presence data for a given topic.


[View source]
def self.reset_presence #

Resets presence tracking. Mainly useful for testing.


[View source]

Instance Method Detail

def after_join(client_socket) #

Called after #handle_joined completes successfully. Override this to perform post-join logic such as sending welcome messages or notifying other users.


[View source]
def after_leave(client_socket) #

Called after #handle_leave completes successfully. Override this to perform cleanup logic after a user leaves a channel.


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


[View source]
def handle_joined(client_socket, message) #

Authorization can happen here


[View source]
def handle_leave(client_socket) #

[View source]
abstract def handle_message(client_socket, msg) #

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

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.


[View source]
def presence_count : Int32 #

Returns the number of sockets currently present in this channel's topic.


[View source]
def presence_list : Hash(String, Hash(String, String)) #

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


[View source]
def rebroadcast!(message, topic = @topic_path) #

[View source]
def subscribe_to_channel(client_socket, message) #

Called when a socket subscribes to a channel


[View source]
def unsubscribe_from_channel(client_socket) #

Called when a socket unsubscribes from a channel


[View source]