# Realtime Connections

The connection a caller's streams and channels share: what it opens with, how it comes back after a dropped network, a hidden page or a restarted server, what each close code means, how it goes on where no WebSocket gets through, and who the server takes it to be.

## Connection options

Every channel and push subscription of one caller on one URL shares a socket. The caller's `ws` option sets that socket up, in `createCaller`, `createProxyCaller`, `createQueryCaller` and `createProxyQueryCaller` alike:

```ts title="lib/client.ts" side="page"
import { createProxyCaller } from "@noverax/courier/client"
import { routes } from "@noverax/courier/routes"

export const client = createProxyCaller(routes.api, {
  ws: {
    auth: async () => ({ token: await session.accessToken() }),
    backoff: { initialMs: 500, maxMs: 10_000 },
    messageQueue: { maxCount: 100, overflow: "drop-oldest" },
  },
})
```

- `auth` — what the client tells the server about itself, as socket.io's `auth` does: an object, or a function that returns one, called anew before every connect and reconnect so the token in it is current. It is sent in the socket's first frame, never in its URL, where proxies and servers log it, and the server's `onConnect` reads it as `auth` — see [Who a connection is](#who-a-connection-is). It is sent again, asked for anew, when the server asks with `connections.requireAuth`; a caller without it leaves that request unanswered, and the server closes the connection at its deadline. What JSON cannot carry — a `BigInt`, as an ORM gives a bigint column, an object that points back at itself — ends the connection with a `ConnectionError` that says so, as no attempt could send it.
- `backoff` — the wait before each reconnect: `initialMs` (1 s), `maxMs` (30 s), `factor` (2), `jitter` (on). See [Staying connected](#staying-connected).
- `fallback` — where no WebSocket gets through, the same connection over HTTP requests to the socket's own path; `false` keeps it on WebSocket. `after` (3 s) is how long a WebSocket the server has not answered on is waited for, and an HTTP stream that brings no bytes; `remember` (on) keeps the rung a connection fell to for the tab's session. See [When no WebSocket gets through](#when-no-websocket-gets-through).
- `messageQueue` — what the socket keeps to send while it cannot: messages sent while it reconnects, and those sent on a channel the server has not confirmed yet. `maxCount` (100) and `maxBytes` (1 MB) hold for all its channels together. `overflow` says what a message that finds no room does: with `"reject"`, the default, it is dropped with a warning and its `send` answers `false`; with `"drop-oldest"`, it is kept, and the oldest message waiting goes instead — its channel hears a `4429` error.
- `reconnectOnResume` — whether the page's signals, and those an app passes on with `resumeRealtime`, act on the socket, `true` unless set to `false`. See [Staying connected](#staying-connected).
- `protocols` — WebSocket sub-protocols for the handshake.

How often the server pings the socket and the largest message it takes are the server's to say: it names both when it admits the socket, and the client goes by them — see [Staying connected](#staying-connected) and [Message size](/docs/advanced/realtime#message-size).

A caller with `ws` options opens a socket of its own, beside the one another caller uses on the same URL, so its options are never dropped in favour of the other's.

## Staying connected

A socket is `connected` once the server has admitted it — `onConnect` accepted its credentials, and the server answered them with `auth_ok` — not as soon as it opens, so a connection the server refuses never reads `connected`. A server that answers in frames of another version of the protocol, as a replica of an older build does while a rollout replaces it, is left at once and called back after a wait.

A connection that drops reconnects on its own, and its channels subscribe again:

- **After a wait.** Each wait is drawn at random between 0 and `initialMs × factor^n`, `n` the attempts that failed in a row before it, and is never longer than `maxMs`, so clients that lost a server together do not come back to it together. A connection that stays open for 10 s counts as having worked: its next drop starts the waits over. A server that asks for a wait — one shutting down, one at capacity — is not called back sooner. With `jitter: false` every wait is the exponential one.
- **At once, when the page says so.** The network comes back (`online`, then a wait of up to a second at random), the page is shown again, it comes back from the back/forward cache, or its window gets focus. A wait the server asked for is kept all the same: the connection comes back up to a second at random after it, and its failed attempts stay counted, as the server's answer has not changed. A connection open at that moment is checked instead: it sends a ping, and a socket that does not answer within 5 s is taken for gone. `reconnectOnResume: false` turns this off. React Native gives no such signals, nor does Node: an app passes its own on with `resumeRealtime` from `@noverax/courier/realtime` — `"visible"` once it is back in the foreground, `"online"` once its network is back — and every connection acts on them as on a page's, a socket that is well staying open.
- **A socket that falls silent** — no frame for the server's ping interval and pong timeout together, as the server named them when it admitted the socket: 35 s for a Courier server's defaults — is taken for gone though it still reads as open: a network that dropped without a word sends no close.

In a React Native app, the app coming back to the foreground is the signal to pass on, once for all its channels and streams:

```ts title="app/realtime.ts"
import { resumeRealtime } from "@noverax/courier/realtime"
import { AppState } from "react-native"

AppState.addEventListener("change", (state) => {
  if (state === "active") resumeRealtime("visible")
})
```

The network coming back is the other, as `@react-native-community/netinfo` reports it: `resumeRealtime("online")`. `reconnect()` stays for a connection the app wants made again whatever its state: it drops a socket that is well too.

A socket that takes longer than 15 s to open, or a server that takes longer than 15 s to admit it, counts as a failed attempt. A Courier server says why it closes a socket in a frame right before the close, and the client goes by that frame's code and wait: a balancer may lose the close itself, but not the frame before it. Some closes are not reconnected, as reconnecting would meet the same answer:

| Code | Why | The client |
| --- | --- | --- |
| `1000` | the server ended the connection | ends its channels as finished |
| `4401` | credentials refused, expired or not renewed in time | asks for its credentials anew, once, and signs in with them if they changed; otherwise ends its channels with an `AuthError` |
| `4403`, `4421` | origin or host not allowed | ends its channels with the error |
| `4409` | replaced by a newer connection of the same user | ends its channels: reconnecting would replace that one in turn |
| `4429` | too many connections from the address or the user, a `RateLimitError` that `onConnect` or `onReauth` throws, or too much sent before the connection was let in | called back after the wait the server names, 10 s for too many connections or a `RateLimitError`, as a place may free meanwhile; with none named, as for too much sent, ends its channels with a `RateLimitError` |
| `1002` | the two ends speak different versions of the protocol | from a server older than the client, reconnects after a wait, as a rollout replaces that server; from a newer one, ends its channels with the error — the page needs loading again |
| `1003`, `1007`, `1008`, `4000`–`4099` | data or a policy the server will not take, or the application said no | ends its channels with the error |
| `4400`–`4499`, those above and `4408` aside | a refusal of what the client sent, as an HTTP `4xx` is: a frame the protocol does not take (`4400`), what the connection names is not there (`4404`), too much (`4413`), what its schema refuses (`4422`) — and so a `RealtimeError` of such a code that `onConnect` throws | ends its channels with the error, as a stream ends on a `4xx` |

Every other close — a dropped network (`1006`), a server going away (`1001`, `1012`), an internal error (`1011`), a slow consumer (`1013`), a timeout (`4408`) — reconnects, no sooner than the wait the server named, if it named one.

A stream reconnects on the same rules, with what an HTTP stream has in place of a socket's, from the `Last-Event-ID` of the last event it read:

- Its waits are drawn as a socket's are, and start over once a stream stayed open 10 s, so a server that ends each stream at once is called back less and less often. A server that asks for a wait — the stream's `retry:` field, `Retry-After` on a `429` or a `503`, read in seconds or as an HTTP date as [a retry policy reads it](/docs/middleware/built-in#retrymiddleware) — is not called back sooner, nor waited for longer than five minutes.
- An attempt has the 15 s a socket has to open, from asking `beforeConnect` for what it opens with to the server's answer: a balancer that took the request and never answers, or a refusal whose body never ends, counts as a failed attempt. What a refusal says is read from its first 64 KiB; a longer one says its status text.
- A line or an event longer than 1 MiB of text ends the stream with a `ConnectionError` of code `1009`, as a socket's message too large is told: a server that never ends one would hold the client's memory for as long as it sends, and reconnecting would read the same event again. Data larger than that goes in several events.
- A `401` is tried once more, at once, when the credentials asked for anew have changed — a session renewed meanwhile; the same credentials, or a second `401`, end the stream with an `AuthError`. Any other refusal from `400` to `499`, `408` and `429` aside, ends it with its error.
- The page's signals bring back a stream that waits to reconnect, once a wait its server asked for is over, as a socket's. A stream has no ping to be checked with, and a Courier server comments on it every 25 s: one open when a signal comes is opened again only if it has heard nothing for longer than its `heartbeatTimeout`, 45 s, as after the computer slept. A stream that hears nothing for that long while the page is in use is opened again too.

`subscribeSSE` from `@noverax/courier/realtime` takes `backoff`, `heartbeatTimeout`, `maxAttempts` and `reconnectOnResume` for a stream opened directly; a caller's streams keep these defaults.

## When no WebSocket gets through

A corporate proxy, an antivirus that inspects TLS, a WAF or a CDN with WebSockets turned off may refuse the upgrade, hold it, or let it through and hold back what the server sends, while plain HTTP through the same entry gets through. The connection then goes on over HTTP requests to the socket's own path, which a Courier server answers there: the same frames, subscriptions and limits, one transport at a time, so the server admits it once.

- **A WebSocket first.** A connection whose WebSocket works sends no request over HTTP, and the page never loads the code that makes them.
- **A stream, at once for a refusal.** A WebSocket refused, or dropped before the server answered on it, gives way at once: what the connection sent on it, its credentials and subscriptions, goes with the request that opens it over HTTP, and the server's frames come on a stream it holds open. A WebSocket whose upgrade is not done in `after`, or whose server has not answered on it by then, gives way then, and is closed.
- **Polls, where a stream is held back.** A stream that brings no bytes in `after` is held by something on the way, as a proxy that buffers what it carries holds it: polls carry the server's frames from then on, each answered as soon as there is one. Where the `fetch` hands an answer over only once all of it has arrived, as React Native's own does, polls carry them from the start, each answer read whole.
- **Each frame whole.** A frame of the server's comes whole however the network splits it, up to 100 MiB of text, the largest message a socket of the `ws` package takes; a larger one closes the connection with `1009`, the server told, as a socket's message too big does. An answer that is not the transport's is not asked for again at once: a `200` that is no event stream, as an entry's sign-in page is, counts as no HTTP transport at the path, and an opening answered with no event of the server's as a socket that did not open. A refusal ends the connection only when its body names the server's own code, as its refusal of an origin names `ORIGIN_NOT_ALLOWED`; a gateway's in front of the server, in whatever words, counts as a dropped network, `1006`, and the connection reconnects.
- **Where it stood.** The page's next connection to the same path starts on the rung the last one reached, and with `remember` so does the page loaded again in the same tab; the URL's query is not kept. Where no HTTP transport answers — a server with `http: false`, or a replica of an earlier release while a rollout runs — the attempt ends there, and the next tries a WebSocket first again, then one request: behind an entry that deals requests out in turn, it reaches a replica that answers. A request of a connection that opened, dealt to such a replica, is made again, and the connection holds: the next request reaches one that answers.
- **Back to WebSocket.** While HTTP carries a connection, a probe checks whether a WebSocket gets through again: a WebSocket to the same path that sends a `ping` before any `auth`, which the server answers with `pong` and admits no connection for. The first goes within 30 s, each after one not answered within twice as long, up to 5 minutes, and the page's resume signals send one at once, one in 30 s at most, unless `reconnectOnResume` is `false`. A probe answered within `after` forgets the rung, and the connection reconnects at once on a WebSocket: its channels subscribe again from where they stood, and a topic whose [log or history](/docs/deployment/replication#resuming-subscriptions) reaches back that far misses nothing and repeats nothing. While it is open, a probe is a socket of its address like any other, counted in `maxConnectionsPerIP`.

`state.transport` of an open connection says what carries it: `"websocket"`, `"stream"` or `"poll"`. A connection whose WebSocket and request over HTTP both failed never opened: its `state.cause.opened` is `false`, and the cause carries what the platform said of the request. A caller whose sockets come from a `socketFactory` of its own climbs no ladder unless `fallback` is given. A React Native app climbs the same ladder: on a stream under `expo/fetch`, on polls under React Native's own `fetch`, with Expo or without.

What the entry in front of the server must let through for it, and what each transport costs the server, is on [HTTP Fallback](/docs/deployment/http-fallback).

## Who a connection is

A browser's WebSocket carries the page's cookies on its handshake, and nothing else: no script can put an `Authorization` or an API key on it, and a token put in the URL ends up in the logs of every proxy and server it passes. So a Courier client says who it is in a frame of its own, the first it sends on every socket — before its subscriptions, on every reconnect too:

- its `ws.auth`, which the server's `onConnect` reads as `auth`;
- the headers it could not put on the handshake, which the server reads as the handshake's own: `onConnect`'s `request` carries them, and so does every subscription's middleware and session. A header the handshake has keeps its value, and none a browser or a proxy sets itself — `Cookie`, `Origin`, `Host`, `Referer`, `Forwarded`, the `Sec-`, `Proxy-` and `X-Forwarded-` headers and those that claim a client address — is taken from the frame. A frame carries at most 64 headers, each a name and a value HTTP can carry; a first frame that breaks those rules is refused, and the connection is closed with `4401`.

Those headers are the ones the caller's calls send: the API's `headers`, the caller's `headers`, and the user's [session](/docs/server-side/session#streams-and-channels) — its bearer token, and on a server its cookies and `forward`'s headers. Where the platform's WebSocket takes headers on its handshake — on a server, in a React Native app — they go there instead, and the frame carries `auth` alone.

```ts title="server.ts"
const server = await createCourierServer({
  sources,
  onConnect: async ({ request, auth }) => {
    const user = await users.byToken(auth?.token ?? bearerOf(request))
    if (!user) throw new Error("Unauthorized")
    return { userId: user.id }
  },
})
```

The server holds what a client sends until `onConnect` has admitted its connection, then handles it in order, so a subscription made as the socket opens is not lost while the user is looked up. What it holds is bounded: twice `maxSubscriptionsPerConnection` frames, four times `maxMessageSize` in all, and no frame larger than `maxMessageSize`. A socket that sends more before it is admitted is closed with `4429`, naming no wait: a Courier client does not come back after it. A socket whose first frame is no `auth` of the protocol's version — a client that is not Courier's, or a page left open from an older build — is refused with `1002` and why; a `ping` before it, which a client checking that its WebSocket gets through sends, is answered with `pong`, and counts among what it may send before it is admitted. One that sends no `auth` within `authTimeoutMs` (3 s) of opening is closed with `4408`.

To have a client prove who it is again on an open connection — its token about to expire — call `connections.requireAuth(connectionId, { timeoutMs })`, or name every connection of a user with a selector, `connections.requireAuth({ meta: { userId } })`, which reaches them on every replica that shares the pub/sub adapter: the client sends its credentials again, asked for anew, and `onReauth` reads them as `onConnect` does and returns the connection's new metadata, which replaces the old: a field it leaves out is gone.

Credentials `onReauth` refuses close the connection at once with `4401` — and so do any the client sends again to a handler with no `onReauth`, since nothing could check them; none sent by the deadline close it too. A request without a `timeoutMs` of its own gives the client the handler's `authRequiredTimeoutMs`, 30 seconds by default. A later request's deadline replaces an earlier one's, and `timeoutMs: 0` asks with none, which lifts it. The credentials a connection sends again are checked one at a time, in the order they came, so the last sent are the ones it holds, and only those sent after a request answer it: once credentials sent before it are taken, the server asks again, with the time left, and a client that was renewing them of itself sends them anew. Once the metadata `onReauth` returns differs from the old — a field added, removed, or not `Object.is` the one it had — the handler's `authorize` runs again for each of the connection's subscriptions, with the new `meta`, and one it now refuses ends with `4403`. Nothing else runs again — no middleware, no `onJoin`, no channel's generator — so what a user may subscribe to is checked in `authorize`. A `RealtimeError` thrown by `onConnect` — `ForbiddenError("Banned")` — closes the connection with its own code and message, and the client goes by that code as the table under [Staying connected](#staying-connected) says: one from `4400` to `4499` but `4401`, `4408` and `4429` ends its channels with the error. Anything else closes it with `4401` "Connection rejected", and the client goes on as after any `4401`: it asks once more, and signs in again only with credentials that changed.

`connections.disconnect(selector, { code, reason })` closes the connections a selector names — a connection id, `{ id: [...] }`, or `{ meta: { userId } }` — on every replica; see [A User's Connections, on Every Replica](/docs/deployment/replication#a-users-connections-on-every-replica).
