View Source Sorcery.PubSub (Sorcery v0.1.0)
Provides a clean interface for publishing and subscribing to events.
Configuration
In your config.exs:
config :sorcery, :pub_sub,
pub_sub: Sorcery.PubSub.PostgresStore,
pub_sub_opts: [
repo: MyApp.Repo,
table_name: "events"
]Or for in-memory storage:
config :sorcery, :pub_sub,
pub_sub: Sorcery.PubSub.MemoryStoreUsage
# Publish an event
event = Sorcery.Event.new(%{
type: "user_registered",
data: %{user_id: "123"},
domain: "users",
instance_id: "instance_1",
domain_sequence_number: 1
})
Sorcery.PubSub.publish(event)
# Subscribe to events
{:ok, events, pagination} = Sorcery.PubSub.subscribe()
# Query specific events
{:ok, events, pagination} = Sorcery.PubSub.subscribe_by_domain("users")
{:ok, events, pagination} = Sorcery.PubSub.subscribe_by_type("user_registered")
Summary
Functions
Returns the configured options for the pub sub.
Publishes one or more events to the configured store.
Starts the configured pub sub.
Subscribes to events for a given subscription.
Types
@type subscription() :: Sorcery.PubSub.Behaviour.subscription()
Functions
@spec pub_sub() :: module()
@spec pub_sub_opts() :: Keyword.t()
Returns the configured options for the pub sub.
@spec publish(Sorcery.Event.t() | [Sorcery.Event.t()]) :: :ok | {:error, term()}
Publishes one or more events to the configured store.
@spec start_link(Keyword.t()) :: GenServer.on_start()
Starts the configured pub sub.
@spec subscribe() :: :ok | {:error, term()}
Subscribes to events for a given subscription.