ExAgent. PubSub behaviour
(ExAgent v3.0.0)
View Source
Behaviour for broadcasting ExAgent.Events.
Stability — Stable.
ExAgent never depends on a concrete PubSub implementation. The default is
ExAgent.PubSub.None (no side-effects, for one-shot use and tests that don't
observe events). ExAgent.PubSub.Local ships an in-process Registry-backed
implementation that works out of the box. ExAgent.PubSub.Phoenix delegates
to Phoenix.PubSub dynamically, with no hard dependency on Phoenix. A custom
implementation just implements this behaviour.
A PubSub is referenced as {module, config} where config is opaque to
ExAgent (e.g. a PubSub server name for Phoenix, or [] for Local/None). The
helper normalize/1 turns the friendly option forms into that tuple.
Summary
Callbacks
Broadcast event to all subscribers of topic.
Subscribe the calling process to topic. Implementations that can't
subscribe (e.g. None) should return {:error, {:subscribe_unsupported, mod}}.
Remove the calling process's subscriptions to a topic.
Functions
Broadcast via the resolved {module, config} tuple.
Resolve a PubSub option into a {module, config} tuple.
Subscribe the calling process to a topic via the resolved tuple. With the Local and Phoenix backends, each call adds a registration and each registration receives a copy of the event.
Unsubscribe the calling process from a topic. The Local and Phoenix backends remove all of its registrations for that topic. Already delivered messages remain in its mailbox.
Callbacks
@callback broadcast(config :: term(), topic :: String.t(), event :: ExAgent.Event.t()) :: :ok | {:error, term()}
Broadcast event to all subscribers of topic.
Subscribe the calling process to topic. Implementations that can't
subscribe (e.g. None) should return {:error, {:subscribe_unsupported, mod}}.
Remove the calling process's subscriptions to a topic.
Functions
@spec broadcast({module(), term()}, String.t(), ExAgent.Event.t()) :: :ok | {:error, term()}
Broadcast via the resolved {module, config} tuple.
A backend's {:error, reason} is returned to the caller. An exception,
exit, throw or unexpected return from the callback yields
{:error, {:pubsub_broadcast_failed, module}}.
Resolve a PubSub option into a {module, config} tuple.
Accepts:
nil/:none→{ExAgent.PubSub.None, []}:local→{ExAgent.PubSub.Local, []}module→{module, []}{module, conf}→ as-is
The resolved module must implement this behaviour.
Subscribe the calling process to a topic via the resolved tuple. With the Local and Phoenix backends, each call adds a registration and each registration receives a copy of the event.
If the callback raises, exits, throws or returns an unexpected value,
returns {:error, {:pubsub_subscription_unconfirmed, :subscribe, module}}.
The registration may already exist. Explicit backend errors are returned as received.
Unsubscribe the calling process from a topic. The Local and Phoenix backends remove all of its registrations for that topic. Already delivered messages remain in its mailbox.
Returns {:error, {:unsubscribe_unsupported, module}} when the backend
does not implement this optional callback. If the callback raises, exits,
throws or returns an unexpected value, returns
{:error, {:pubsub_subscription_unconfirmed, :unsubscribe, module}}.
The registration may already have been removed. Explicit backend errors are returned as received.