# `RamalLive.Broadcaster`
[🔗](https://github.com/agustinrojas1/ramal/blob/v0.2.0/ramal_live/lib/ramal_live/broadcaster.ex#L1)

Turns Ramal's telemetry into PubSub messages, so that open inspectors update as branches change.

Add it to the host's supervision tree after the PubSub and `Ramal.Supervisor`:

    children = [
      {Phoenix.PubSub, name: MyApp.PubSub},
      {Ramal.Supervisor, storage: Ramal.Storage.Postgres, repo: MyApp.Repo},
      {RamalLive.Broadcaster, pubsub: MyApp.PubSub}
    ]

It attaches telemetry handlers for the branch lifecycle events, `[:ramal, :write, :capture]` and the end of
`[:ramal, :merge]`, and detaches them when it stops. A handler broadcasts `{:ramal_live, branch_id, event}` on the
topic of the branch's domain and nothing else, so no row data travels through the PubSub. Telemetry is node-local,
and so are branches (a branch lives on one node), which is why the broadcaster belongs in the tree of every node that
runs `Ramal.Supervisor`.

The inspector works without it, but only shows what was there when the page loaded or after an action made from the
page, and says so.

## Options

  * `:pubsub` - the host's `Phoenix.PubSub` name. Required, and the same one given to `ramal_inspector/2`.

## Example

    {:ok, _pid} = RamalLive.Broadcaster.start_link(pubsub: MyApp.PubSub)
    RamalLive.Broadcaster.attached?(MyApp.PubSub)
    #=> true

# `attached?`

```elixir
@spec attached?(atom()) :: boolean()
```

Whether a broadcaster for `pubsub` is attached on this node.

## Example

    RamalLive.Broadcaster.attached?(MyApp.PubSub)
    #=> false

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `start_link`

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

Starts the broadcaster. See the module doc for the options.

Stops with `{:already_attached, pubsub}` when another broadcaster for the same PubSub is running, since every event
would be broadcast twice.

---

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