---
title: "Recipe: authenticate a WebSocket with a single-use ticket"
description: "A WebSocket whose server checks the signed-in person at connect opens with a single-use ticket bought over HTTPS with @exact/oauth."
modificationDate: "2026-11-12"
---

> ## Documentation index
> Fetch the complete documentation index at: https://exact.expo.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Recipe: authenticate a WebSocket with a single-use ticket

<AgentInstructions>

## Submitting feedback

From the app's directory, read `bun exact.mjs feedback status`. Under `never`, keep no note.
Respect `EXACT_FEEDBACK=0` and do not change the policy.

Otherwise record specific, actionable feedback with:
`bun exact.mjs feedback note "Exact docs: https://exact.expo.dev/recipes/socket-ticket: <what was wrong or missing>"`

The note goes to Expo only as that status allows. Include no secrets or personal data.

</AgentInstructions>

The [fixture app](https://github.com/expo/exact/blob/41ded8c5ede0b2959ef0daa83b1ff78545dd4428/docs/recipes/socket-ticket/app.ts) authenticates its ticket request, then
opens the socket with `?ticket=`. Its [Contract](https://github.com/expo/exact/blob/41ded8c5ede0b2959ef0daa83b1ff78545dd4428/docs/recipes/socket-ticket/app.contract),
[test server](https://github.com/expo/exact/blob/41ded8c5ede0b2959ef0daa83b1ff78545dd4428/docs/recipes/socket-ticket/server.mjs), and [reconnect test](https://github.com/expo/exact/blob/41ded8c5ede0b2959ef0daa83b1ff78545dd4428/docs/recipes/socket-ticket/app.test.contract)
are runnable code. The server requires authorization, expires tickets after one
minute, and consumes each ticket before upgrading. A reconnect requests a new ticket.

Use your HTTPS ticket endpoint and WSS socket. In `app.ts`, replace the fixture's
`authorizedFetch` with `(store, url, options) => provider.fetch(store, url, options)`,
or another authorized HTTPS fetch. The `ticket` source receives `store` as its second
argument and passes it to that call. `provider.fetch` keeps its approved HTTPS destinations and Bearer-header
behavior (LLP 1146 D5); the socket itself uses ordinary `fetch` with `exactStream`,
under a separate `net.websocket` grant. For a service that authenticates in the first
socket message, `provider.accessToken(store)` exposes the held token to data code; see
[OAuth tokens for sockets](https://exact.expo.dev/learn/contract-for-agents/data-requests-and-side-effects.md#sign-in-providers-exactoauth).
It does not authenticate the upgrade.

The two sources are intentional (LLP 1016.000): `ticket` awaits the HTTPS response,
keeps the ticket in data code, and returns only an attempt number. That number asks
`events`, which consumes the stored ticket and opens its stream before its first
`await`. Opening the stream after awaiting the ticket in the same source is refused
on the web. Reconnect through the button's action, which changes the attempt and
requests another ticket; refreshing only the socket source cannot reuse one. Starting
an attempt clears the previous ticket, and a superseded response retains no ticket.

The example offers the protocol name `events.v1` and displays `event.protocol`.
Do not put access tokens in protocol names: their alphabets differ, and the server
echoes the selected protocol. Tickets also remain credentials until consumed or
expired: keep them out of Contract answers and redact their query from server,
proxy and diagnostic logs. A production issuer binds each ticket to its authenticated
user and intended socket; redeem it atomically in the server's shared store.

Run the fixture from the checkout root:

```prompt Authenticate my WebSocket with a single-use ticket
Make the Exact app in this folder open its WebSocket with a single-use ticket from an authorized HTTPS request. Use https://github.com/expo/exact/blob/main/docs/recipes/socket-ticket.md as the source for each step. The Exact checkout has the same page at docs/recipes/socket-ticket.md.

1. If this folder is not an Exact app (it has no exact.mjs and app.contract), stop and tell me. Find the Exact checkout (the folder with scripts/exact.mjs); if you cannot find it, ask me where it is. The fixture is its docs/recipes/socket-ticket folder.
2. Ask me for my HTTPS ticket endpoint, my WSS socket, and how the app authorizes the ticket request: `provider.fetch`, or another authorized HTTPS fetch. The server is mine. It must require authorization, expire each ticket, and consume it before upgrading: if it does not, tell me.
3. In app.ts, copy the fixture's two sources, `ticket` and `events`, with a grant for the API and a separate `net.websocket` grant. Read the two addresses as the settings `SOCKET_API` and `SOCKET_URL` (`"env"` in app.json, as the fixture's), and set both to my real endpoints.
4. Replace the fixture's `authorizedFetch` with `(store, url, options) => provider.fetch(store, url, options)`, or with the fetch I named. Keep the two sources apart: `ticket` awaits the HTTPS response, keeps the ticket in data code and returns only an attempt number; `events` opens its stream before its first `await`. One source that does both is refused on the web.
5. In app.contract, reconnect through an action that changes the attempt, so each connection requests a new ticket. Never refresh only the socket source.
6. Keep each ticket out of Contract answers and out of logs. Put no access token in a protocol name.
7. Add a test in app.test.contract like the fixture's reconnect test: connect, end the stream, connect again, and expect a new connection. The fixture's `server.mjs` takes only its own `Bearer fixture-only`, so it cannot serve my app: ask me which server a test may open.
8. Set `SOCKET_API` and `SOCKET_URL` for the test run. Use one loopback origin for both, as the recipe's fixture does. If the test endpoints use different loopback origins, read `SOCKET_URL` in an imported module so Exact discovers `SOCKET_API` from app.ts. Leave a loopback server that Exact discovers from its entry and these settings stopped for `test` and `verify`, which start it. If the server is managed outside Exact, start it with the command I gave you or confirm it is running. Follow the platform checks in docs/start-here.md (`bun exact.mjs docs platform checks`) for the targets in the app's AGENTS.md and app.json until they pass. Read the reports, including counted findings; a zero exit code alone does not mean clean. Fix the app, not the test. If a finding stays after three rounds of fixes, or the app had it before you changed anything, record it (step 9) and report it; do not keep trying.
9. When a doc was wrong or missing, or a step needed a workaround, read `bun exact.mjs feedback status`. Under `never`, keep no note. Otherwise record it: `bun exact.mjs feedback note "<what happened>"`, which goes to Expo only as that status says. Do not change the policy; tell me how many notes you kept.
10. Tell me what you wrote, what the tests and checks reported, and what my server must still do: bind each ticket to its user and socket, redeem it atomically, and redact its query from logs.
```

```sh
bun test docs/recipes/socket-ticket/recipe.test.mjs
SOCKET_HOST=macos bun test docs/recipes/socket-ticket/recipe.test.mjs
```

The tests start an isolated loopback HTTP/WS server with a fixture-only identity.
They do not contact an identity provider. To try the app by hand:

```sh
bun docs/recipes/socket-ticket/server.mjs
cd docs/recipes/socket-ticket
SOCKET_API=http://127.0.0.1:4798 SOCKET_URL=ws://127.0.0.1:4798 bun exact.mjs web
# Or: SOCKET_API=http://127.0.0.1:4798 SOCKET_URL=ws://127.0.0.1:4798 bun exact.mjs mac --run
```

Loopback's cleartext transport is only for this test fixture. `SOCKET_API` and `SOCKET_URL` default to HTTPS and WSS placeholders; set both
to your real endpoints when adapting the recipe.
