---
title: "Recipe: a Convex backend (live queries, mutations)"
description: "@exact/convex keeps Convex queries live over one socket, with nothing polled."
modificationDate: "2026-11-27"
---

> ## 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: a Convex backend (live queries, mutations)

<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/convex: <what was wrong or missing>"`

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

</AgentInstructions>

`@exact/convex` updates live queries in one commit over one socket. Install
`EXACT_APP_DIR=$PWD bun <exact2>/scripts/exact.mjs add convex`, `bun add convex`,
then `npx convex dev`. See `bun exact.mjs docs convex live queries` for errors,
tokens, tests and the HTTP client.

```prompt Use Convex as my app's backend
Add a Convex backend with live queries and mutations to the Exact app in this folder. Use https://github.com/expo/exact/blob/main/docs/recipes/convex.md as the source for each step. The Exact checkout has the same page at docs/recipes/convex.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.
2. If I did not say which data the app keeps in Convex, propose the queries and mutations and ask me before you write code. Ask me for my deployment's socket origin. `npx convex dev` is Convex's own: if it asks you to sign in or to make a project, stop and tell me. Live queries run on the web, macOS, iOS, Linux and Windows: tell me if the app targets another.
3. Install the package: `EXACT_APP_DIR=$PWD bun <exact2>/scripts/exact.mjs add convex`, then `bun add convex`, then `npx convex dev`.
4. Declare `CONVEX_SOCKET_URL` in app.json's `env` and use its `ws://` or `wss://` value for both the `net.websocket` grant and the `convex` client. Set it to the active development deployment's socket origin for interactive runs and to my deployment's socket origin for release builds. A test or check build reads it as any build does: the environment, then `.env.local`, then `.env`, then the default in app.ts. The Convex CLI owns the HTTP setting `CONVEX_URL` in `.env.local`. Follow the recipe for `cx.live()`, `cx.query` with `rev` first, and `cx.mutation` with a reply for a `ConvexFailure`. Shape results in `cx.query`'s fourth argument.
5. In app.contract, write `use ConvexLive from "@exact/convex"` at the top, one `live` resource at the root, and `live.rev` in every query. Show `live.status`. Copy the recipe's `reconnect` and `recovered` tasks and their actions. Write no `refreshes` after a mutation.
6. If the app needs sign-in, read `bun exact.mjs docs convex auth` and tell me what I must set up before you write it. Call `cx.setAuth(jwt)` in a mutation's source, after any `fetch` it awaits.
7. Check for an existing test server or declared fixture before choosing the stand-in setup. Keep that server's port and routes. Adapt the setup in docs/recipes/convex.md and apps/mosaic/README.md to the app's servers. See apps/mosaic/server.ts, the socket origin in Mosaic's app.ts, and the server declaration in its app.json. Add a test in app.test.contract that sends a mutation and expects a query to show what it wrote.
8. Use only the local Convex stand-in throughout automated tests and checks, including their builds and drives. Run each with the stand-in's socket origin in the environment, such as `CONVEX_SOCKET_URL=ws://127.0.0.1:3210 bun exact.mjs test web`: with a cloud origin in `.env.local`, the tests run against that deployment and no stand-in starts. 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. Read the reports, including counted findings. Fix findings in the app. Record (step 9) and report pre-existing findings and any that remain after three rounds of fixes.
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 the queries and mutations, what the tests and checks reported, and what I must still do in Convex.
```

```ts
import { convex, ConvexFailure } from '@exact/convex';
import { getFunctionName } from 'convex/server';
import { convexToJson, jsonToConvex } from 'convex/values';
import { api } from './convex/_generated/api';

// The grant and client share the socket origin.
const CONVEX = 'wss://happy-otter-123.convex.cloud';
export const grants = `net.websocket ${CONVEX}`;

const cx = convex(CONVEX, { getFunctionName, convexToJson, jsonToConvex });
const sources: Sources = {
  live: () => cx.live(),
  messages: ([channel, rev]) => cx.query(rev, api.messages.list, { channel }),
  send: ([channel, body]) => cx.mutation(api.messages.send, { channel, body }).then(
    () => ({ ok: true, error: '' }),
    (e) => ({ ok: false, error: e instanceof ConvexFailure ? e.message : 'Try again.' })),
};
```

```text
use ConvexLive from "@exact/convex"   // at the top of the file

component Chat
  resource live = live() as shape ConvexLive else empty(status="connecting")
  resource messages = messages(channel, live.rev) as shape Message[] else empty()
  mutation sent as shape Reply then afterSend
  state waited = 0
  state wait = 1
  task reconnect when live.status == "closed"
    every(1000, retry)
  task recovered when live.status == "ready" and wait > 1
    after(1000, calm)
  action retry
    if waited + 1 >= wait
      waited = 0
      wait = min(wait * 2, 16)
      refresh live
    else
      waited = waited + 1
  action calm
    wait = 1
    waited = 0
```

- **One `live` resource, at the root:** the socket. Show `live.status`:
  `connecting`, `open`, `ready`, `closed` (`retry` refreshes it) or `failed`.
- **Every query takes `live.rev`,** first, in `cx.query`. When a result
  changes, `rev` rises and each query answers again at once.
- **No `refreshes` after a write:** a mutation's reply arrives once the queries
  show what it wrote.
- **Shape a result in the same call,** `cx.query`'s fourth argument: `(b) => ({
  tiles: b.cells.map(…) })`. An `async` wrapper makes every change a pending
  frame.
- **`cx.setAuth(jwt)`** in a mutation's source, after any `fetch` it awaits:
  a source that does both before its first `await` is refused.
- **A drive against a real deployment** takes a new `--seed` each
  launch, or Convex answers the second launch's writes without running them.

Use `apps/mosaic/server.ts` as a model for tests. `test` and `verify` start
it only when the build's socket origin is a loopback one, as Mosaic's is.

## Convex Auth v2

Call the deployment's mutations and use `openAuthSession` for the browser step.
State in `redirectTo`, `convexAuthCode`, resuming, renewing and signing out:
`bun exact.mjs docs convex auth`. Native setup:
[HTTPS callbacks](https://exact.expo.dev/learn/contract-for-agents/data-requests-and-side-effects.md#https-callbacks).
