abstract class Amber::Adapters::PubSubAdapter

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

Instance Method Summary

Instance Method Detail

def active_topics : Array(String) #

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.


[View source]
abstract def close : Nil #

Closes the adapter and cleans up any resources (connections, background processes, etc.) Should be called when the application is shutting down.


[View source]
def healthy? : Bool #

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.


[View source]
abstract def publish(topic : String, sender_id : String, message : JSON::Any) : Nil #

Publishes a message to all subscribers of the specified topic.

  • topic: The topic/channel to publish to
  • sender_id: Unique identifier of the message sender (usually client socket ID)
  • message: The message content to publish

[View source]
abstract def subscribe(topic : String, &block : String, JSON::Any -> Nil) : Nil #

Subscribes to messages on the specified topic. The provided block will be called for each received message.

  • topic: The topic/channel to subscribe to
  • block: Callback that receives (sender_id, message) for each published message

Note: The sender_id allows subscribers to filter out their own messages if needed.


[View source]
def subscriber_count : Int32 #

Returns the number of active subscribers across all topics. This is useful for monitoring and debugging. Override if your backend can provide this efficiently.


[View source]
abstract def unsubscribe(topic : String) : Nil #

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

[View source]
abstract def unsubscribe_all : Nil #

Unsubscribes from all topics and cleans up any resources. This should be called when shutting down the adapter.


[View source]