# `Snodo.Subscription.Bridge`
[🔗](https://github.com/joshrotenberg/snodo/blob/v0.5.0/lib/snodo/subscription/bridge.ex#L1)

A supervised `:pg` bridge between node-local subscription hubs.

Start the same scope and group on each directly connected Erlang node that
serves subscriptions. The child spec starts the scope, a local
`Snodo.Subscription.Hub`, and a forwarding worker in that order under a
`:rest_for_one` supervisor. The default `start_scope: :auto` monitors a scope
that is already registered when the bridge starts, without starting a
replacement during this bridge supervisor's lifetime. Otherwise the bridge
starts and owns the scope. If a parent restarts the whole bridge, `:auto` is
evaluated again and may start and own the scope if it is then absent. Use
`start_scope: true` to allow starting a replacement even for an existing
scope, or `false` to require an externally supervised scope. Put the scope
first and its bridges after it under a
`:rest_for_one` parent so a scope failure restarts the bridges after a live
replacement is available. A hub failure restarts the forwarding worker.
If the hub is manually stopped and restarted, the worker monitors it and
resolves its replacement on the next publication.

    children = [{Snodo.Subscription.Bridge, name: MyApp.SubscriptionBridge}]

    runtime =
      MyServer.runtime(
        subscription_source: Snodo.Subscription.Bridge.source(MyApp.SubscriptionBridge)
      )

    Snodo.Subscription.Bridge.publish(
      MyApp.SubscriptionBridge,
      Snodo.Subscription.Event.tools_list_changed()
    )

Publications are validated and delivered locally before being forwarded.
The returned report describes only the local hub; remote sends have no
acknowledgement. Each node's hub keeps its own bounded per-listener queues,
filters, and overflow policy. The forwarding worker's mailbox is not bounded
by the hub's queue limit.

`:pg` membership converges after nodes connect directly. A new bridge can
miss events until peers learn about it. Events published during a network
split are not replayed after reconnection. Restarting a node or its local
scope or hub loses in-memory listeners and queued events. Clients should
reopen streams and refresh affected catalogs or resources.

This is one `Snodo.Subscription.Source`; applications using another message
bus can implement that behaviour or publish into their own local hubs.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `open`

```elixir
@spec open(map(), Snodo.Context.t(), Supervisor.supervisor()) ::
  {:ok, map(), term()} | {:error, :invalid_filter | :unavailable}
```

Opens a listener on the current local hub.

Returns `{:error, :unavailable}` if the supervisor or hub is absent,
restarting, or exits during the call.

# `publish`

```elixir
@spec publish(Supervisor.supervisor(), Snodo.Subscription.Event.t()) ::
  {:ok, Snodo.Subscription.Hub.delivery_report()}
  | {:error, {:invalid_event, String.t()} | :unavailable}
```

Publishes locally and forwards a valid event to every other group member.

The delivery report covers only the local hub. Remote delivery is best effort
and is not acknowledged or replayed. Returns `{:error, :unavailable}` if the
supervisor, forwarding worker, or hub is absent, restarting, or exits during
the call. If the hub is absent, this returns `{:error, :unavailable}` before
validating the event. An event may already have reached listeners if an
in-flight call fails.

# `source`

```elixir
@spec source(Supervisor.supervisor()) :: {module(), Supervisor.supervisor()}
```

Returns a source configuration that resolves the current local hub when opening a stream.

# `start_link`

```elixir
@spec start_link(keyword()) :: Supervisor.on_start()
```

Starts the scope, local hub, and forwarding worker under one supervisor.

Options are:

  * `:name` - the supervisor's registered name, defaulting to `Snodo.Subscription.Bridge`;
    use `nil` for an unnamed supervisor;
  * `:scope` - the `:pg` scope atom, defaulting to `Snodo.Subscription.Bridge.PG`;
  * `:start_scope` - `:auto` (the default) monitors an already registered scope
    without starting replacements during this supervisor's lifetime, or
    starts and owns an absent scope.
    `true` allows starting the scope on every restart, while `false` requires
    an externally supervised scope;
  * `:group` - the `:pg` group term, defaulting to `{Snodo.Subscription.Bridge, :events}`;
  * `:hub_options` - options passed to `Snodo.Subscription.Hub.start_link/1`,
    defaulting to `[]` (a buffer of 100 events per listener and `:drop_oldest`).

Use the same scope and group names on every participating node. Different
groups may share a scope. Stopping a bridge stops a scope it started, so
supervise a shared scope separately when bridges have independent lifetimes.

With `start_scope: false`, the caller must start the scope first. If it is
absent when the scope child starts, this returns:

    {:error, {:scope_not_started, scope}}

With `start_scope: :auto`, the same error can occur if an existing scope exits
between the registration check and the scope child's startup.

If the scope exits later during startup, this may fail on a later child or
succeed and then restart as usual. The usual supervisor startup and linking
semantics apply. The bridge never starts or waits for an external scope. If
it stays absent after a failure, immediate restart failures exhaust the
bridge supervisor's restart intensity and the supervisor exits. Put the scope
and its bridges under a `:rest_for_one` parent, with the scope first, so it
restarts the scope before restarting the bridges.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
