# `Macaw.Live.Stream`
[🔗](https://github.com/dcoai/macaw_live/blob/v0.1.0/lib/macaw/live/stream.ex#L1)

The binary stream (spec/05 §9): one Phoenix channel per page, beside its LiveView,
joined by the page's `MacawSurface` hook, carrying a contained value's changed drawing
segments as binary frames (`Macaw.Live.Stream.Frame`), so a live update sends its bytes
raw and its markup only each segment's key and token. The first render, and every render
while no channel is joined, carries the segments as base64 in markup, so a page without
a socket, or whose channel is refused, is complete and never fails (spec/00 §2). A
neutral or print render carries no stream and no drawing token: it is static (spec/05
§9.1).

**The host declares it** in the socket its `LiveSocket` connects to, beside LiveView's
own channels:

    defmodule MyAppWeb.LiveSocket do
      use Phoenix.LiveView.Socket
      channel "mw:*", Macaw.Live.Stream
    end

A host that doesn't declare it refuses every join, and its pages stay base64.

**The seam a component draws through** (spec/05 §9.1) is `segments/2`: a contained
component exporting `segments/1` hands the runtime its drawing as `{token, [{key, token,
iodata}]}`, and the runtime decides each segment's carrier.

**Joining:** a connected page renders its topic, `"mw:<id>"` (`topic/1`, the LiveView's
id), and a token signed by the endpoint naming the page's process (`token/3`) on each
surface's wrapper; the hook joins the topic once per page with `{"token": token}`. The
channel verifies the token, monitors the page and tells it its pid; a token that doesn't
verify, names another topic or a page that has ended is refused, `{:error, %{reason:
"refused"}}`. The channel ends with its page.

# `drawing`

```elixir
@type drawing() :: {String.t(), [segment()]}
```

A value's drawing: its token and its segments in drawing order (spec/05 §9.1).

# `segment`

```elixir
@type segment() :: {String.t(), String.t(), iodata()}
```

One drawing segment as a value hands it over: `{key, token, iodata}` (spec/05 §9.1).

# `child_spec`

# `event`

```elixir
@spec event() :: String.t()
```

The event a frame is pushed as, `"s"` (spec/05 §9.2).

# `segments`

```elixir
@spec segments(module(), term()) ::
  {:ok, drawing()} | :none | {:error, {:segments, [Macaw.Base.Fault.t(), ...]}}
```

The drawing a contained value hands the runtime (spec/05 §9.1), read through its
component's optional `segments/1`: `{:ok, drawing}` for `{token, [{key, token, iodata}]}`,
each token and key a string of 1 to 255 bytes and every key once; `:none` for a component
that doesn't export `segments/1`; else `{:error, {:segments, faults}}`, the faults
`{[:token], …}` or `{[:segments, i, field], …}` as `Macaw.Live.Stream.Frame.encode/5`
words them, `{[:segments, i], {:duplicate_key, key}}`, or `{[], {:type, :drawing}}` for
an answer of another shape. A contained cell answering such an error stops with it, as
one answering a request of no allowed shape does (spec/02 §5).

# `start_link`

# `token`

```elixir
@spec token(module(), String.t(), pid()) :: String.t()
```

The token a page renders for its stream (spec/05 §9.3): its topic's id and its process,
signed by `endpoint` as LiveView signs its own session, valid for a day.

# `topic`

```elixir
@spec topic(String.t()) :: String.t()
```

The topic of a page's stream, `"mw:<id>"`, `id` its LiveView's id (spec/05 §9.3).

    iex> Macaw.Live.Stream.topic("phx-F1")
    "mw:phx-F1"

---

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