# `ALLM.Providers.Support.WebSocket.Mint`
[🔗](https://github.com/cykod/ALLM/blob/v0.6.1/lib/allm/providers/support/web_socket/mint.ex#L1)

The default `ALLM.Providers.Support.WebSocket` implementation, over
`Mint.WebSocket`.

Layer B — runtime. `Mint.WebSocket` is process-less: the socket belongs
to the process that calls `connect/3`, and its `:tcp` / `:ssl` messages
arrive in that process's mailbox. The streaming audio adapters call every
function from the process that reduces the stream, so a halted stream
owns the socket it has to close.

## Connecting

`connect/3` derives the scheme, host and port from the URL:

| URL | Transport | Default port |
|-----|-----------|--------------|
| `wss://…` | TLS (`:https`), upgrade scheme `:wss` | 443 |
| `ws://…` | plain TCP (`:http`), upgrade scheme `:ws` | 80 |

The connection is always HTTP/1. The handshake runs in the calling
process with a `receive` that selects only this socket's messages, so no
other message in the caller's mailbox is consumed, and it is bounded by
`opts[:connect_timeout]` (default 10,000 ms). A response other than 101
returns `{:error, {:upgrade_status, status, body}}`, where `body` is the
JSON-decoded response body when it is JSON and the raw binary otherwise.

The bundled adapters build `wss://` URLs from their `https://` base URL.
The plain `ws://` scheme is what a `http://` base URL produces, which is
how the tests reach a local server; TLS is exercised only by the live
recorders, the same split as `Finch` versus the test stubs.

## Messages

Every message this connection delivers is a 2- or 3-tuple whose second
element is the socket (`message_tag/1`): `{:tcp, socket, data}`,
`{:tcp_closed, socket}`, `{:tcp_error, socket, reason}` and their `:ssl`
counterparts. `handle_message/2` answers a server ping with a pong
itself and drops pongs. `flush_messages/1` removes all of them from the
mailbox after `close/1`.

# `t`

```elixir
@type t() :: %ALLM.Providers.Support.WebSocket.Mint{
  conn: term(),
  pending: binary(),
  ref: term(),
  socket: term(),
  websocket: term()
}
```

An open connection. `pending` holds bytes that arrived in the same read
as the 101 response and have not been decoded yet.

---

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