# `MuroPlug.WebSocket`
[🔗](https://github.com/murolang/muro_plug/blob/main/lib/muro_plug/websocket.ex#L1)

WebSocket (RFC 6455) over MuroPlug, through the `WebSock` API.

A Plug upgrades a request with `WebSockAdapter.upgrade/4` (what
Phoenix does for sockets and LiveView) or, directly, with

    Plug.Conn.upgrade_adapter(conn, :websocket, {MyWebSock, state, opts})

The request head was parsed and validated by the book (step.muro) like
any other request. `MuroPlug.Adapter.upgrade/3` then checks the
RFC 6455 §4.2.1 opening handshake (`check/3` here) and keeps the
`WebSock` spec on the payload. When the Plug returns,
`MuroPlug.Handler` writes `101 Switching Protocols` (with the response
headers the Plug set, such as `set-cookie` or `sec-websocket-protocol`)
and runs `MuroPlug.WebSocket.Connection` on the same
`ThousandIsland.Socket`, so `ws://` and `wss://` are one code path.

A handshake that fails the checks raises
`MuroPlug.WebSocket.UpgradeError`: `400 Bad Request`, or
`426 Upgrade Required` with `sec-websocket-version: 13` for a version
other than 13.

Options (the `opts` of the upgrade tuple; the `WebSockAdapter` names):

  * `:timeout` — idle milliseconds with no client data before the
    server closes with 1001 and `terminate(:timeout, state)`.
    Defaults to `60_000`.
  * `:max_frame_size` — the largest frame payload accepted, in octets
    (or `:infinity`); a larger frame closes with 1009. Defaults to
    `10_000_000`.
  * `:max_message_size` — the largest assembled (possibly fragmented)
    message, in octets (or `:infinity`); larger closes with 1009.
    Defaults to the `:max_frame_size` value.
  * `:validate_utf8` — check that text messages and close reasons are
    UTF-8 (1007 when not). Uncompressed text is checked as each
    fragment arrives and as payload bytes of a frame arrive: a
    prefix that cannot be valid UTF-8 closes before the rest of the
    frame. Compressed text is checked after inflate. Defaults to
    `true`.
  * `:close_timeout` — after the server sends a close frame, how long
    to wait for the client's close before dropping the socket.
    Defaults to `5_000`.
  * `:fullsweep_after`, `:max_heap_size` — process flags for the
    connection process. A `max_heap_size` map must have a
    non-negative integer `:size` and may have `:kill`,
    `:error_logger`, and `:include_shared_binaries` booleans
    (Erlang's `Process.flag/2`); unknown keys or a negative `:size`
    raise `ArgumentError` before the 101.
  * `:compress` — negotiate RFC 7692 `permessage-deflate` when the
    client offers it. `false` (the default) never advertises
    `Sec-WebSocket-Extensions`. `true`, or a keyword list of
    `:zlib` deflate options (`:level`, `:mem_level`, `:strategy`,
    and `:window_bits` 9..15), accepts a valid offer and compresses
    outgoing data frames (RSV1). Control frames are never compressed.
    `:deflate_options` (or `:deflate_opts`) is the same keyword list
    when `:compress` is `true`. `:window_bits` caps the server's
    deflate window and is advertised as `server_max_window_bits`
    when it is below 15 or below the client's offer (`client_max_window_bits`
    may still be 8..15; `server_max_window_bits=8` is skipped
    because `:zlib` cannot deflate with a raw window of 8).
    Frames with RSV1 set without a negotiated extension,
    or RSV1 on a continuation or control frame, close with 1002.
    A corrupt deflate payload or invalid UTF-8 after inflate closes
    with 1007; inflated bytes past `:max_message_size` close with
    1009.

Known options with bad values (e.g. `close_timeout: :infinity`)
raise `ArgumentError` from the upgrade, before the 101; unknown keys
are ignored.

# `accept`

```elixir
@spec accept(binary()) :: binary()
```

`Sec-WebSocket-Accept` for a client's `Sec-WebSocket-Key`.

# `check`

```elixir
@spec check(binary(), boolean(), [{binary(), binary()}]) ::
  {:ok, binary()} | {:error, 400 | 426, binary(), [{binary(), binary()}]}
```

Check an RFC 6455 §4.2.1 opening handshake: `GET`, HTTP/1.1, a Host,
`Upgrade` with the `websocket` token and `Connection` with the
`upgrade` token (both case-insensitive, in any header line),
`Sec-WebSocket-Version: 13`, and one `Sec-WebSocket-Key` that is
base64 of 16 octets. Returns `{:ok, key}` or `{:error, status,
message, headers}`.

---

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