abstract class
Amber::Adapters::PubSubAdapter
- Amber::Adapters::PubSubAdapter
- Reference
- Object
Overview
Abstract base class for pub/sub messaging adapters used by WebSocket channels.
All pub/sub implementations should inherit from this class and implement the required abstract methods. This allows the Amber framework to work with any messaging backend (Redis, in-memory, database, message queues, etc.) through a unified interface.
Example implementation:
class CustomPubSubAdapter < Amber::Adapters::PubSubAdapter
def initialize(@message_broker : MyMessageBroker)
end
def publish(topic : String, sender_id : String, message : JSON::Any) : Nil
@message_broker.send_message(topic, {sender: sender_id, msg: message}.to_json)
end
# ... implement other abstract methods
end
Direct Known Subclasses
Defined in:
amber/adapters/pubsub_adapter.crInstance Method Summary
-
#active_topics : Array(String)
Returns a list of all active topics that have subscribers.
-
#close : Nil
Closes the adapter and cleans up any resources (connections, background processes, etc.) Should be called when the application is shutting down.
-
#healthy? : Bool
Returns true if the adapter is healthy and ready to handle pub/sub operations.
-
#publish(topic : String, sender_id : String, message : JSON::Any) : Nil
Publishes a message to all subscribers of the specified topic.
-
#subscribe(topic : String, &block : String, JSON::Any -> Nil) : Nil
Subscribes to messages on the specified topic.
-
#subscriber_count : Int32
Returns the number of active subscribers across all topics.
-
#unsubscribe(topic : String) : Nil
Unsubscribes from the specified topic.
-
#unsubscribe_all : Nil
Unsubscribes from all topics and cleans up any resources.
Instance Method Detail
Returns a list of all active topics that have subscribers. This is useful for monitoring and debugging. Override if your backend can provide this efficiently.
Closes the adapter and cleans up any resources (connections, background processes, etc.) Should be called when the application is shutting down.
Returns true if the adapter is healthy and ready to handle pub/sub operations. Override this method to implement health checks for your messaging backend.
Publishes a message to all subscribers of the specified topic.
topic: The topic/channel to publish tosender_id: Unique identifier of the message sender (usually client socket ID)message: The message content to publish
Subscribes to messages on the specified topic. The provided block will be called for each received message.
topic: The topic/channel to subscribe toblock: Callback that receives (sender_id, message) for each published message
Note: The sender_id allows subscribers to filter out their own messages if needed.
Returns the number of active subscribers across all topics. This is useful for monitoring and debugging. Override if your backend can provide this efficiently.
Unsubscribes from the specified topic. After calling this, the subscriber should no longer receive messages for this topic.
topic: The topic/channel to unsubscribe from
Unsubscribes from all topics and cleans up any resources. This should be called when shutting down the adapter.