Jido.Messaging.PubSub (Jido Messaging v1.0.0)

Copy Markdown View Source

Optional Phoenix.PubSub integration for cross-node events and LiveView support.

This module provides publish/subscribe functionality for room events when Phoenix.PubSub is available. All functions gracefully handle cases where PubSub is not configured.

Usage

defmodule MyApp.Messaging do
  use Jido.Messaging, persistence: Jido.Messaging.Persistence.ETS,
    pubsub: MyApp.PubSub
end

# Subscribe to room events
MyApp.Messaging.subscribe("room_123")

# Events received:
# {:message_added, %Message{}}
# {:participant_joined, %Participant{}}
# {:participant_left, participant_id}

Summary

Functions

Broadcast an event to all subscribers of a room.

Check if PubSub is configured for the given instance module.

Check if Phoenix.PubSub is available at runtime.

Subscribe to events for a room.

Generate the topic string for a room.

Unsubscribe from events for a room.

Functions

broadcast(instance_module, room_id, event)

@spec broadcast(module(), String.t(), term()) :: :ok | {:error, :not_configured}

Broadcast an event to all subscribers of a room.

Returns :ok on success, or {:error, :not_configured} if PubSub is not configured.

configured?(instance_module)

@spec configured?(module()) :: boolean()

Check if PubSub is configured for the given instance module.

pubsub_available?()

@spec pubsub_available?() :: boolean()

Check if Phoenix.PubSub is available at runtime.

subscribe(instance_module, room_id)

@spec subscribe(module(), String.t()) :: :ok | {:error, :not_configured}

Subscribe to events for a room.

Returns :ok on success, or {:error, :not_configured} if PubSub is not configured.

topic(room_id)

@spec topic(String.t()) :: String.t()

Generate the topic string for a room.

unsubscribe(instance_module, room_id)

@spec unsubscribe(module(), String.t()) :: :ok | {:error, :not_configured}

Unsubscribe from events for a room.

Returns :ok on success, or {:error, :not_configured} if PubSub is not configured.