Source: https://h2corn.monicz.dev/guide/websockets/

# WebSockets

h2corn supports WebSockets over HTTP/1.1 and HTTP/2:

- Classic [RFC 6455](https://datatracker.ietf.org/doc/html/rfc6455) WebSockets over HTTP/1.1 with the `Upgrade` handshake.
- [RFC 8441](https://datatracker.ietf.org/doc/html/rfc8441) "Bootstrapping WebSockets with HTTP/2", using the extended `CONNECT` method on a single HTTP/2 stream.

To the application, both look identical: a `websocket` ASGI scope, a `receive` callable, and a `send` callable. The transport is negotiated between client and server with no application code changes.

## Echo application

ws.py

```python
from fastapi import FastAPI, WebSocket
from starlette.websockets import WebSocketDisconnect

app = FastAPI()


@app.websocket('/ws')
async def echo(ws: WebSocket):
    await ws.accept()
    try:
        while True:
            message = await ws.receive_text()
            await ws.send_text(f'echo: {message}')
    except WebSocketDisconnect:
        pass
```

Once the peer is gone, `send()` raises [`SendAfterCloseError`](https://h2corn.monicz.dev/api/server/#h2corn.SendAfterCloseError "SendAfterCloseError") (a subclass of `OSError`) — see [What `send()` raises](https://h2corn.monicz.dev/asgi/#what-send-raises). [Starlette](https://www.starlette.io/) and [FastAPI](https://fastapi.tiangolo.com/) surface that as `WebSocketDisconnect`. For typed message contracts, see [WebSocket scope and messages](https://h2corn.monicz.dev/api/websockets/).

## Why the HTTP/2 transport matters

On HTTP/2, each WebSocket is a stream on the shared connection rather than a hijacked socket. Two practical consequences:

- A client can multiplex many WebSockets and ordinary HTTP requests on one TCP connection, instead of opening a fresh socket per stream.
- The proxy → app hop stays on HTTP/2 the whole time — no `Upgrade`/`Connection: keep-alive` interaction with HTTP/1.1.

## Limits and keep-alives

See [WebSocket options](https://h2corn.monicz.dev/configuration/#websocket) and [`Config`](https://h2corn.monicz.dev/api/config/#h2corn.Config "Config") for defaults and CLI flags:

- [`websocket_max_message_size`](https://h2corn.monicz.dev/configuration/#option-websocket_max_message_size) caps a reassembled message, not one frame: fragments are accounted as they arrive, and a compressed message is measured after decompression. Set it to `none` on the CLI (or `None` in Python) to remove the cap entirely.
- [`websocket_per_message_deflate`](https://h2corn.monicz.dev/configuration/#option-websocket_per_message_deflate) controls whether the server accepts the [permessage-deflate](https://datatracker.ietf.org/doc/html/rfc7692) compression extension when a client offers it.
- [`websocket_ping_interval`](https://h2corn.monicz.dev/configuration/#option-websocket_ping_interval) and [`timeout_websocket_ping`](https://h2corn.monicz.dev/configuration/#option-timeout_websocket_ping) keep idle connections alive and detect dead peers. Setting `websocket_ping_interval` to `none` on the CLI (or `None` in Python) disables both pings and their timeout.

## HTTP/2-only deployment

development command

```bash
h2corn ws:app --no-http1
```

With [`--no-http1`](https://h2corn.monicz.dev/configuration/#option-http1), the server only accepts the HTTP/2 WebSocket bootstrap — the [RFC 8441](https://datatracker.ietf.org/doc/html/rfc8441) extended `CONNECT`. The WebSocket client and proxy path must support RFC 8441; HTTP/1.1 clients instead need a proxy that translates their `Upgrade` request. If that path is unavailable, route WebSocket upgrades over HTTP/1.1 and ordinary requests over `h2c`, and keep HTTP/1 enabled there. See [Behind a reverse proxy](https://h2corn.monicz.dev/guide/proxy/#behind-a-reverse-proxy) before dropping HTTP/1.
