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

Usage

# 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

subscription()

@type subscription() :: Sorcery.PubSub.Behaviour.subscription()

Functions

child_spec(opts)

pub_sub()

@spec pub_sub() :: module()

pub_sub_opts()

@spec pub_sub_opts() :: Keyword.t()

Returns the configured options for the pub sub.

publish(events)

@spec publish(Sorcery.Event.t() | [Sorcery.Event.t()]) :: :ok | {:error, term()}

Publishes one or more events to the configured store.

start_link(opts \\ [])

@spec start_link(Keyword.t()) :: GenServer.on_start()

Starts the configured pub sub.

subscribe()

@spec subscribe() :: :ok | {:error, term()}

Subscribes to events for a given subscription.