@neon/skills 3.0.10 → 3.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/plugin.json +1 -1
- package/skills/neon/SKILL.md +4 -4
- package/skills/neon-realtime/SKILL.md +3 -2
- package/skills/neon-realtime/references/mutations.md +8 -6
- package/skills/neon-realtime/references/queries.md +12 -8
- package/skills/neon-realtime/references/reactivity.md +11 -9
- package/skills/neon-realtime/references/ssr.md +4 -2
- package/skills/neon-realtime/references/subscribing.md +35 -31
package/package.json
CHANGED
package/plugin.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json",
|
|
3
3
|
"name": "neon-postgres",
|
|
4
|
-
"version": "3.0.
|
|
4
|
+
"version": "3.0.11",
|
|
5
5
|
"description": "Manage your Neon backend with the Neon agent skills and the Neon MCP Server: Lakebase Postgres, Managed Better Auth, Realtime, Object Storage, Functions, the Data API, and AI Gateway",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Neon",
|
package/skills/neon/SKILL.md
CHANGED
|
@@ -512,14 +512,14 @@ Enroll in the [Neon Agent Program](https://neon.com/programs/agents.md) only whe
|
|
|
512
512
|
|
|
513
513
|
Report issues that Neon can fix upstream, such as incorrect guidance, broken templates, missing instructions, or defects in Neon tooling and APIs. Do not report errors caused only by the user's code, environment, permissions, or configuration unless Neon guidance or these agent skills caused the issue or failed to explain it.
|
|
514
514
|
|
|
515
|
-
Diagnose the issue and fully help the user first.
|
|
515
|
+
Diagnose the issue and fully help the user first. Feedback must not delay or replace helping the user. After the task is complete, draft one report with the cause and workaround when known, show it to the user, and ask whether to send it to Neon. Send it only if they say yes.
|
|
516
|
+
|
|
517
|
+
After a yes, run `neon feedback --help`. If it succeeds, send the report with:
|
|
516
518
|
|
|
517
519
|
```bash
|
|
518
520
|
neon feedback --message "Describe what failed, including the command and sanitized error message."
|
|
519
521
|
```
|
|
520
522
|
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
The goal is to improve the user's experience without interrupting or getting in the way of their current work. If submission fails, do not retry. Finish the user's task normally.
|
|
523
|
+
Otherwise use the Neon MCP `send_feedback` tool when it is present. If neither is available, skip feedback. Do not install, upgrade, authenticate, or configure tooling only to send feedback. If submission fails, do not retry.
|
|
524
524
|
|
|
525
525
|
**Never send secrets or sensitive data.** Remove credentials, tokens, API keys, passwords, personal or customer data, and confidential project details from the report. Send only the issue report described above; do not collect or infer analytics.
|
|
@@ -154,6 +154,7 @@ Migrating from Electric: Realtime is a drop-in replacement for Electric's role i
|
|
|
154
154
|
## Setup
|
|
155
155
|
|
|
156
156
|
- **New app:** `neon bootstrap my-app --template realtime-react`, then `neon deploy` in `my-app`.
|
|
157
|
+
- **Existing app:** follow the [Next.js example](https://neon.com/docs/realtime/examples/nextjs.md). Its setup declares Realtime with `neon config add realtime` and runs `neon deploy`, and [Adapt the example to your app](https://neon.com/docs/realtime/examples/nextjs.md#adapt-the-example-to-your-app) covers raw SQL with `createRealtime()` and no adapter, and apps with their own auth or none.
|
|
157
158
|
- **Enable** it on the linked branch with [`neon realtime enable --allowed-origin <app origin>`](https://neon.com/docs/cli/realtime.md), or declare [`preview: { realtime: true }`](https://neon.com/docs/reference/neon-ts.md#realtime) in `neon.ts` (`neon config add realtime`) and run `neon deploy`. Child branches inherit it. It's unavailable on Claimable projects until claimed, or outside the [supported regions](https://neon.com/docs/realtime/overview.md#which-regions-is-it-enabled-in).
|
|
158
159
|
- **Tables** need a [stable replica identity](references/queries.md#table-requirements); a primary key is enough. Leave [row-level security](references/queries.md#why-not-row-level-security) off on every table a live query reads: Realtime rejects those subscriptions with `unsupported query: row security`. For per-user data, authorize in the sealed query instead (see below).
|
|
159
160
|
- **Environment:** `neon env pull --service postgres --service realtime` writes `DATABASE_URL`, `NEON_REALTIME_URL` and `NEON_REALTIME_SECRET`. Expose only the URL to the browser, under your framework's public prefix (like `VITE_`). `url` options take `NEON_REALTIME_URL`; `db` takes the database name from `DATABASE_URL`.
|
|
@@ -200,7 +201,7 @@ Read [references/ssr.md](references/ssr.md) before rendering live queries on the
|
|
|
200
201
|
| Beta scope, regions, pricing, FAQ, other systems | [Overview](https://neon.com/docs/realtime/overview.md) |
|
|
201
202
|
| Architecture and consistency | [How it works](https://neon.com/docs/realtime/how-it-works.md) |
|
|
202
203
|
| A first live query | [Quickstart](https://neon.com/docs/realtime/quickstart.md) |
|
|
203
|
-
| Every SDK export | [TypeScript SDK
|
|
204
|
-
| Full-stack apps with auth and SSR | [Next.js](https://neon.com/docs/realtime/examples/nextjs.md), [TanStack](https://neon.com/docs/realtime/examples/tanstack.md) |
|
|
204
|
+
| Every SDK export, one page per module | [TypeScript SDK overview](https://neon.com/docs/realtime/sdks/typescript.md): [Backend](https://neon.com/docs/realtime/sdks/typescript/backend.md), [Client](https://neon.com/docs/realtime/sdks/typescript/client.md), [React](https://neon.com/docs/realtime/sdks/typescript/react.md), [TanStack DB](https://neon.com/docs/realtime/sdks/typescript/tanstack-db.md), [Drizzle and Drizzle client](https://neon.com/docs/realtime/sdks/typescript/drizzle.md), [Kysely](https://neon.com/docs/realtime/sdks/typescript/kysely.md) |
|
|
205
|
+
| Full-stack apps with auth and SSR, or Realtime in an existing Next.js app | [Next.js](https://neon.com/docs/realtime/examples/nextjs.md), [TanStack](https://neon.com/docs/realtime/examples/tanstack.md) |
|
|
205
206
|
| Live agent workflows | [Mastra](https://neon.com/docs/realtime/examples/mastra.md) |
|
|
206
207
|
| ORM adapters | [Drizzle](https://neon.com/docs/realtime/examples/drizzle.md), [Kysely](https://neon.com/docs/realtime/examples/kysely.md) |
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<!-- Generated by scripts/sync-skill-docs.mjs from website@
|
|
1
|
+
<!-- Generated by scripts/sync-skill-docs.mjs from website@HEAD:e520dd136+dirty (docs/realtime/guides/mutations). Do not edit: change the docs and run npm run sync:docs. sha256:01fbd1e6a58f3c8ab13b010a70558be0ac5e93e85791fed6310a4c1d7a19e448 -->
|
|
2
2
|
Canonical page: https://neon.com/docs/realtime/guides/mutations.md
|
|
3
3
|
|
|
4
4
|
> Summary: Write through your own backend, return the transaction id, apply the change optimistically and drop the optimistic state once the change arrives on the live query. Without a framework, with React useOptimistic or with TanStack DB.
|
|
@@ -21,6 +21,8 @@ Make apps and agents super fast, with instant local writes.
|
|
|
21
21
|
- [With TanStack DB collections](#with-tanstack-db-collections)
|
|
22
22
|
- [Render the app](#render-the-app)
|
|
23
23
|
|
|
24
|
+
Last updated: 2026-11-18
|
|
25
|
+
|
|
24
26
|
Apply instant local writes using [optimistic mutations](#how-optimistic-mutations-work) that [automatically discard their optimistic state](#automatic-management-of-optimistic-state) when the changes [sync in through your live query subscriptions](subscribing.md#listen-for-changes).
|
|
25
27
|
|
|
26
28
|
## How optimistic mutations work
|
|
@@ -35,9 +37,9 @@ Realtime allows you to automate the management and lifecycle of your optimistic
|
|
|
35
37
|
|
|
36
38
|
1. apply the change to local, optimistic state, so the UI updates straight away
|
|
37
39
|
2. send the write through your backend, with your normal validation, authorization and backend logic, which returns the [ID of the transaction](https://www.postgresql.org/docs/current/transaction-id.html) that made it
|
|
38
|
-
3. wait
|
|
40
|
+
3. wait until the subscription has synced that transaction, using [`awaitTxId()`](https://neon.com/docs/realtime/sdks/typescript/client.md#client-materializedlivequerysubscriptionawaittxid)
|
|
39
41
|
4. drop the optimistic state
|
|
40
|
-
5. if the write fails, or
|
|
42
|
+
5. if the write fails, or the transaction doesn't sync before the timeout, drop the optimistic state and show an error
|
|
41
43
|
|
|
42
44
|
`awaitTxId()` remembers recently seen transaction IDs, so it resolves even when the change arrives before your backend's HTTP response. It also resolves after the subscription's `onChange`, so the synced rows already include the change when the optimistic state drops, which means nothing flickers.
|
|
43
45
|
|
|
@@ -215,7 +217,7 @@ When an edit or a delete batch matches no rows, the statement returns no row, so
|
|
|
215
217
|
|
|
216
218
|
**Important: Always pass a timeout**
|
|
217
219
|
|
|
218
|
-
`awaitTxId()` keeps waiting while the subscription reconnects or waits for a renewed sealed query,
|
|
220
|
+
`awaitTxId()` keeps waiting while the subscription reconnects or waits for a renewed sealed query, so always pass a timeout. When the timeout passes, it rejects, the optimistic change drops and the UI shows that the change couldn't be confirmed. The write may still have committed. It also rejects if the subscription errors or closes.
|
|
219
221
|
|
|
220
222
|
### Calling the routes from the frontend
|
|
221
223
|
|
|
@@ -524,7 +526,7 @@ export function Messages({ sealedQuery, refreshQuery, initialData, channelId, us
|
|
|
524
526
|
}
|
|
525
527
|
```
|
|
526
528
|
|
|
527
|
-
`mutate()` runs each mutation in its own transition, so independent writes run concurrently. The transition applies the mutation optimistically, writes through your backend and, if the write committed, waits for its txid with [`utils.awaitTxId()`](https://neon.com/docs/realtime/sdks/typescript.md#react-uselivequeryutils). When the transition ends, React drops its optimistic change. By then the change is in `data`, with the same id for a sent message, so the list doesn't flicker. If the write fails or isn't confirmed in time, the optimistic change drops the same way, the message reverts and the error line shows.
|
|
529
|
+
`mutate()` runs each mutation in its own transition, so independent writes run concurrently. The transition applies the mutation optimistically, writes through your backend and, if the write committed, waits for its txid with [`utils.awaitTxId()`](https://neon.com/docs/realtime/sdks/typescript/react.md#react-uselivequeryutils). When the transition ends, React drops its optimistic change. By then the change is in `data`, with the same id for a sent message, so the list doesn't flicker. If the write fails or isn't confirmed in time, the optimistic change drops the same way, the message reverts and the error line shows.
|
|
528
530
|
|
|
529
531
|
Edit and Delete show only on the user's own messages, and the routes still enforce authorship. They're disabled on pending messages, so you don't start a new change on a message whose last change isn't confirmed yet.
|
|
530
532
|
|
|
@@ -608,7 +610,7 @@ export function createMessagesCollection(sealedQuery: SealedLiveQuery<Message>)
|
|
|
608
610
|
export type MessagesCollection = ReturnType<typeof createMessagesCollection>
|
|
609
611
|
```
|
|
610
612
|
|
|
611
|
-
While a handler runs, TanStack DB holds back synced changes. When the handler returns, it drops the optimistic state and applies the synced rows together, so a sent message's optimistic row gives way to its synced row, which has the same id, in a single step. If a handler throws, the optimistic change rolls back. [`collection.utils.awaitTxId()`](https://neon.com/docs/realtime/sdks/typescript.md#tanstack-db-realtimecollectionutilsawaittxid) resolves once the transaction has entered TanStack DB's sync queue, and it takes the same explicit timeout.
|
|
613
|
+
While a handler runs, TanStack DB holds back synced changes. When the handler returns, it drops the optimistic state and applies the synced rows together, so a sent message's optimistic row gives way to its synced row, which has the same id, in a single step. If a handler throws, the optimistic change rolls back. [`collection.utils.awaitTxId()`](https://neon.com/docs/realtime/sdks/typescript/tanstack-db.md#tanstack-db-realtimecollectionutilsawaittxid) resolves once the transaction has entered TanStack DB's sync queue, and it takes the same explicit timeout.
|
|
612
614
|
|
|
613
615
|
Then write through the collection. This is the optimistic version of `frontend/Inbox.tsx` from the [reactivity guide](reactivity.md#query-the-collection). It runs the same local query, and its `insert()`, `update()` and `delete()` calls apply each change locally at once and call the matching handler:
|
|
614
616
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<!-- Generated by scripts/sync-skill-docs.mjs from website@
|
|
1
|
+
<!-- Generated by scripts/sync-skill-docs.mjs from website@HEAD:e520dd136+dirty (docs/realtime/guides/queries). Do not edit: change the docs and run npm run sync:docs. sha256:e965803575c1edde217e413c6f1df44caa8ec72ac673a460a622780ac9324f0f -->
|
|
2
2
|
Canonical page: https://neon.com/docs/realtime/guides/queries.md
|
|
3
3
|
|
|
4
4
|
> Summary: Define live queries in your backend with raw SQL or Drizzle, fill in their parameters from the authenticated request, and seal them so clients can subscribe without being able to read or change them.
|
|
@@ -24,6 +24,8 @@ Define live SQL queries that you can subscribe to.
|
|
|
24
24
|
- [Exposing an endpoint](#exposing-an-endpoint)
|
|
25
25
|
- [Using server functions](#using-server-functions)
|
|
26
26
|
|
|
27
|
+
Last updated: 2026-11-18
|
|
28
|
+
|
|
27
29
|
Realtime works by defining and [subscribing](subscribing.md) to live SQL queries. Queries are designed to be [defined securely](#defining-live-queries) in your backend code and [subscribed‑to safely](#exposing-queries) in your frontend.
|
|
28
30
|
|
|
29
31
|
Define live queries using [raw SQL](#using-raw-sql) or a [query builder or ORM](#using-a-query-builder-or-orm). [Seal the queries](#sealing-queries) and [expose](#exposing-queries) them to your frontend via an [API endpoint](#exposing-an-endpoint) or a [server function](#using-server-functions), using your normal [authorization](#authorization) context.
|
|
@@ -43,7 +45,7 @@ npm install drizzle-orm pg hono jose ws
|
|
|
43
45
|
npm install -D @types/pg @types/ws
|
|
44
46
|
```
|
|
45
47
|
|
|
46
|
-
Install the Realtime [TypeScript SDK](https://neon.com/docs/realtime/sdks/typescript.md) and its [Drizzle adapter](https://neon.com/docs/realtime/sdks/typescript.md
|
|
48
|
+
Install the Realtime [TypeScript SDK](https://neon.com/docs/realtime/sdks/typescript.md) and its [Drizzle adapter](https://neon.com/docs/realtime/sdks/typescript/drizzle.md):
|
|
47
49
|
|
|
48
50
|
```bash filename="Terminal"
|
|
49
51
|
npm install @neon/realtime @neon/realtime-drizzle
|
|
@@ -161,6 +163,8 @@ So, for example, you can't `SELECT` over updated rows and you can't use function
|
|
|
161
163
|
|
|
162
164
|
Tables need a [stable replica identity](https://www.postgresql.org/docs/current/sql-altertable.html#SQL-ALTERTABLE-REPLICA-IDENTITY). If your tables have a primary key, they're fine.
|
|
163
165
|
|
|
166
|
+
Tables can't have [row-level security](https://neon.com/docs/guides/row-level-security.md) enabled. A live query on a table with RLS is rejected: the subscription moves to `error` with the code `upstream_rejected`. With [debug mode](subscribing.md#show-full-database-errors) on, the message says the table has row-level security enabled. Filter rows in the query instead, as shown in [Authorization](#authorization).
|
|
167
|
+
|
|
164
168
|
**Optional: REPLICA IDENTITY FULL:**
|
|
165
169
|
|
|
166
170
|
Setting `REPLICA IDENTITY FULL` can make live queries marginally faster, at the cost of writing the whole old row to the Postgres WAL on every update and delete. That may save Realtime a lookup when it processes those changes. If latency matters for a write-heavy table, benchmark both settings with your own workload before you switch.
|
|
@@ -184,7 +188,7 @@ export const realtime = createRealtime({
|
|
|
184
188
|
|
|
185
189
|
`createRealtime()` also takes a [`debugMode`](subscribing.md#show-full-database-errors) option.
|
|
186
190
|
|
|
187
|
-
Then define queries using [`rawSql`](https://neon.com/docs/realtime/sdks/typescript.md#backend-rawsql). For example:
|
|
191
|
+
Then define queries using [`rawSql`](https://neon.com/docs/realtime/sdks/typescript/backend.md#backend-rawsql). For example:
|
|
188
192
|
|
|
189
193
|
```ts filename="backend/queries-raw.ts"
|
|
190
194
|
import { rawSql } from "@neon/realtime/server"
|
|
@@ -228,7 +232,7 @@ Never use literal interpolation for user input like `` `WHERE user_id = '${userI
|
|
|
228
232
|
|
|
229
233
|
#### Parameter types
|
|
230
234
|
|
|
231
|
-
You can pass strings, numbers, booleans, bigints and `null` as they are. Other value types need a [`pgParam`](https://neon.com/docs/realtime/sdks/typescript.md#backend-pgparam) helper to specify the Postgres type. A `Date` could be a `date`, a `timestamp` or a `timestamptz`, so wrap it in `pgParam.date()`, `pgParam.timestamp()` or `pgParam.timestamptz()`.
|
|
235
|
+
You can pass strings, numbers, booleans, bigints and `null` as they are. Other value types need a [`pgParam`](https://neon.com/docs/realtime/sdks/typescript/backend.md#backend-pgparam) helper to specify the Postgres type. A `Date` could be a `date`, a `timestamp` or a `timestamptz`, so wrap it in `pgParam.date()`, `pgParam.timestamp()` or `pgParam.timestamptz()`.
|
|
232
236
|
|
|
233
237
|
For example, this filters messages `since` a [timestamp with timezone](https://neon.com/postgresql/tutorial/timestamp#introduction-to-postgresql-timestamp):
|
|
234
238
|
|
|
@@ -254,11 +258,11 @@ There are helpers for dates, JSON, `bytea` and arrays, such as `pgParam.array("t
|
|
|
254
258
|
|
|
255
259
|
### Using a query builder or ORM
|
|
256
260
|
|
|
257
|
-
Realtime supports an adapter pattern to convert any typed query object to raw SQL. This works by passing an adapter to [`createRealtime`](https://neon.com/docs/realtime/sdks/typescript.md#backend-createrealtime).
|
|
261
|
+
Realtime supports an adapter pattern to convert any typed query object to raw SQL. This works by passing an adapter to [`createRealtime`](https://neon.com/docs/realtime/sdks/typescript/backend.md#backend-createrealtime).
|
|
258
262
|
|
|
259
263
|
The rest of these guides use the Drizzle versions of `backend/realtime.ts` and `backend/queries.ts` below in place of the raw SQL files above.
|
|
260
264
|
|
|
261
|
-
For example, configure Realtime to use the [`drizzleAdapter`](https://neon.com/docs/realtime/sdks/typescript.md#drizzle-drizzleadapter):
|
|
265
|
+
For example, configure Realtime to use the [`drizzleAdapter`](https://neon.com/docs/realtime/sdks/typescript/drizzle.md#drizzle-drizzleadapter):
|
|
262
266
|
|
|
263
267
|
```ts filename="backend/realtime.ts" {2,7}
|
|
264
268
|
import { createRealtime } from "@neon/realtime/server"
|
|
@@ -314,13 +318,13 @@ To seal a query you pass your query instance, e.g.:
|
|
|
314
318
|
const query = messagesQuery(userId, channelId)
|
|
315
319
|
```
|
|
316
320
|
|
|
317
|
-
To the [`seal`](https://neon.com/docs/realtime/sdks/typescript.md#backend-realtimeserverseal) method of the `RealtimeServer` instance you created above:
|
|
321
|
+
To the [`seal`](https://neon.com/docs/realtime/sdks/typescript/backend.md#backend-realtimeserverseal) method of the `RealtimeServer` instance you created above:
|
|
318
322
|
|
|
319
323
|
```ts filename="backend/server.ts"
|
|
320
324
|
const sealedQuery = await realtime.seal({ query })
|
|
321
325
|
```
|
|
322
326
|
|
|
323
|
-
It's local [Web Crypto](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) work with no network call, so it's cheap to run on every request. It returns a [`SealedLiveQuery<Row>`](https://neon.com/docs/realtime/sdks/typescript.md#backend-sealedlivequery), which has three fields:
|
|
327
|
+
It's local [Web Crypto](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) work with no network call, so it's cheap to run on every request. It returns a [`SealedLiveQuery<Row>`](https://neon.com/docs/realtime/sdks/typescript/backend.md#backend-sealedlivequery), which has three fields:
|
|
324
328
|
|
|
325
329
|
- `capability`: the encrypted query
|
|
326
330
|
- `expiresAt`: when it expires, in Unix milliseconds
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<!-- Generated by scripts/sync-skill-docs.mjs from website@
|
|
1
|
+
<!-- Generated by scripts/sync-skill-docs.mjs from website@HEAD:e520dd136+dirty (docs/realtime/guides/reactivity). Do not edit: change the docs and run npm run sync:docs. sha256:ac050d9a74a806eb5acd080446c816adcaeddc76e378d0a623e9239198480fc6 -->
|
|
2
2
|
Canonical page: https://neon.com/docs/realtime/guides/reactivity.md
|
|
3
3
|
|
|
4
4
|
> Summary: Bind live queries to React component state with useLiveQuery, change their parameters and handle errors, or sync a broader live query into a TanStack DB collection and derive instant, local queries from it.
|
|
@@ -21,6 +21,8 @@ Bind live queries to component and agent state.
|
|
|
21
21
|
- [Manage optimistic state](#manage-optimistic-state)
|
|
22
22
|
- [Other frameworks](#other-frameworks)
|
|
23
23
|
|
|
24
|
+
Last updated: 2026-11-18
|
|
25
|
+
|
|
24
26
|
Build reactive apps and agents using higher-level reactivity primitives that manage live query [subscriptions](subscribing.md) for you.
|
|
25
27
|
|
|
26
28
|
**Important: Framework support**
|
|
@@ -33,7 +35,7 @@ This page continues the [running example](https://github.com/neondatabase/exampl
|
|
|
33
35
|
|
|
34
36
|
## React
|
|
35
37
|
|
|
36
|
-
The `@neon/realtime-react` binding exports a [`RealtimeProvider`](https://neon.com/docs/realtime/sdks/typescript.md#react-realtimeprovider), a [`useLiveQuery()` hook](https://neon.com/docs/realtime/sdks/typescript.md#react-uselivequery) and a [`useRealtimeClient()` hook](https://neon.com/docs/realtime/sdks/typescript.md#react-userealtimeclient) that returns the provider's client. It works with React 18 and 19.
|
|
38
|
+
The `@neon/realtime-react` binding exports a [`RealtimeProvider`](https://neon.com/docs/realtime/sdks/typescript/react.md#react-realtimeprovider), a [`useLiveQuery()` hook](https://neon.com/docs/realtime/sdks/typescript/react.md#react-uselivequery) and a [`useRealtimeClient()` hook](https://neon.com/docs/realtime/sdks/typescript/react.md#react-userealtimeclient) that returns the provider's client. It works with React 18 and 19.
|
|
37
39
|
|
|
38
40
|
Install it alongside the client SDK:
|
|
39
41
|
|
|
@@ -43,7 +45,7 @@ npm install @neon/realtime-react
|
|
|
43
45
|
|
|
44
46
|
### RealtimeProvider
|
|
45
47
|
|
|
46
|
-
Wrap your app in `<RealtimeProvider>`. This allows all of your `useLiveQuery()` subscriptions to use the same [`RealtimeClient`](https://neon.com/docs/realtime/sdks/typescript.md#client-realtimeclient) and WebSocket connection.
|
|
48
|
+
Wrap your app in `<RealtimeProvider>`. This allows all of your `useLiveQuery()` subscriptions to use the same [`RealtimeClient`](https://neon.com/docs/realtime/sdks/typescript/client.md#client-realtimeclient) and WebSocket connection.
|
|
47
49
|
|
|
48
50
|
```tsx filename="frontend/main.tsx"
|
|
49
51
|
import { StrictMode } from "react"
|
|
@@ -65,7 +67,7 @@ createRoot(document.getElementById("root")!).render(
|
|
|
65
67
|
|
|
66
68
|
### useLiveQuery
|
|
67
69
|
|
|
68
|
-
[`useLiveQuery()`](https://neon.com/docs/realtime/sdks/typescript.md#react-uselivequery) takes a `sealedQuery` and a `refreshQuery` (and, optionally, [`initialData` from SSR](ssr.md)) and returns the query's current rows and state.
|
|
70
|
+
[`useLiveQuery()`](https://neon.com/docs/realtime/sdks/typescript/react.md#react-uselivequery) takes a `sealedQuery` and a `refreshQuery` (and, optionally, [`initialData` from SSR](ssr.md)) and returns the query's current rows and state.
|
|
69
71
|
|
|
70
72
|
For example, here we define a `Messages` component with `useLiveQuery`:
|
|
71
73
|
|
|
@@ -147,11 +149,11 @@ The hook also accepts an optional `initialData` parameter, for example from a se
|
|
|
147
149
|
|
|
148
150
|
#### utils
|
|
149
151
|
|
|
150
|
-
The `utils` in the return value give you access to the hook's underlying [materialized subscription](https://neon.com/docs/realtime/sdks/typescript.md#react-uselivequeryutils).
|
|
152
|
+
The `utils` in the return value give you access to the hook's underlying [materialized subscription](https://neon.com/docs/realtime/sdks/typescript/react.md#react-uselivequeryutils).
|
|
151
153
|
|
|
152
154
|
This provides `getSnapshot()`, `getState()`, `awaitRows()`, the `onChange`, `onUpdate` and `onStateChange` listeners, `renew()` and `awaitTxId()`. It's stable for the life of the subscription, so register listeners in an effect and return the remover, as the `onUpdate` listener does.
|
|
153
155
|
|
|
154
|
-
Note that a `changes` update has already been applied by the time its listener runs, and it [carries the txids](https://neon.com/docs/realtime/sdks/typescript.md#client-livequerybatchinfo) of the transactions in it, which the [mutations guide](mutations.md#how-optimistic-mutations-work) uses to confirm optimistic writes.
|
|
156
|
+
Note that a `changes` update has already been applied by the time its listener runs, and it [carries the txids](https://neon.com/docs/realtime/sdks/typescript/client.md#client-livequerybatchinfo) of the transactions in it, which the [mutations guide](mutations.md#how-optimistic-mutations-work) uses to confirm optimistic writes.
|
|
155
157
|
|
|
156
158
|
`utils` omits `unsubscribe()`, because React owns cleanup. The hook unsubscribes when the component unmounts or its sealed query changes. `getState()` and the listeners report the state of the underlying subscription, so they can differ from the hook's `status`.
|
|
157
159
|
|
|
@@ -308,13 +310,13 @@ export function createMessagesCollection(sealedQuery: SealedLiveQuery<Message>)
|
|
|
308
310
|
export type MessagesCollection = ReturnType<typeof createMessagesCollection>
|
|
309
311
|
```
|
|
310
312
|
|
|
311
|
-
`getKey` returns each message's primary key. TanStack DB keys rows by it, so use a stable, unique column, never a subscription's `rowId`. `refreshQuery` renews the sealed query the same way [the React hook does](#refreshquery). While renewals fail, the collection stays `ready` with its last rows. See the [`realtimeCollectionOptions()` reference](https://neon.com/docs/realtime/sdks/typescript.md#tanstack-db-realtimecollectionoptions) for the rest of the options.
|
|
313
|
+
`getKey` returns each message's primary key. TanStack DB keys rows by it, so use a stable, unique column, never a subscription's `rowId`. `refreshQuery` renews the sealed query the same way [the React hook does](#refreshquery). While renewals fail, the collection stays `ready` with its last rows. See the [`realtimeCollectionOptions()` reference](https://neon.com/docs/realtime/sdks/typescript/tanstack-db.md#tanstack-db-realtimecollectionoptions) for the rest of the options.
|
|
312
314
|
|
|
313
315
|
The collection starts syncing as soon as a live query using it is mounted. It's in the `loading` state until the first result arrives, and `ready` from then on, with each committed transaction applied atomically. A `ready` collection stays ready while the subscription is `connecting` or `stale`, so there's no separate recovery flag to render. If the subscription ends in `error`, so does the collection.
|
|
314
316
|
|
|
315
317
|
### Query the collection
|
|
316
318
|
|
|
317
|
-
Create the collection once, when the app starts. The collection takes the client directly,
|
|
319
|
+
Create the collection once, when the app starts. The collection takes the client directly, so an app that only uses collections doesn't need `RealtimeProvider`. `Channel` still calls `useLiveQuery()`, though, so keep both views inside `RealtimeProvider`:
|
|
318
320
|
|
|
319
321
|
```tsx filename="frontend/main.tsx"
|
|
320
322
|
import { StrictMode } from "react"
|
|
@@ -402,6 +404,6 @@ For writes through the collection, with optimistic state, see the [mutations gui
|
|
|
402
404
|
|
|
403
405
|
Bindings for other frameworks are coming. Until then, you can use the current implementations as a reference to write your own.
|
|
404
406
|
|
|
405
|
-
The React binding is a thin layer over the client subscription: `useSyncExternalStore` plus the [`QueryRefreshController`](https://neon.com/docs/realtime/sdks/typescript.md#client-queryrefreshcontroller). Point a coding agent at [its source](https://github.com/neondatabase/neon-pkgs/tree/main/packages/realtime-react) and it can reproduce the binding for other frameworks, such as Solid, Svelte or Vue.
|
|
407
|
+
The React binding is a thin layer over the client subscription: `useSyncExternalStore` plus the [`QueryRefreshController`](https://neon.com/docs/realtime/sdks/typescript/client.md#client-queryrefreshcontroller). Point a coding agent at [its source](https://github.com/neondatabase/neon-pkgs/tree/main/packages/realtime-react) and it can reproduce the binding for other frameworks, such as Solid, Svelte or Vue.
|
|
406
408
|
|
|
407
409
|
You can also see the [source code for the TanStack DB collection here](https://github.com/neondatabase/neon-pkgs/blob/main/packages/realtime-tanstack/src/collection-options.ts).
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<!-- Generated by scripts/sync-skill-docs.mjs from website@
|
|
1
|
+
<!-- Generated by scripts/sync-skill-docs.mjs from website@HEAD:e520dd136+dirty (docs/realtime/guides/ssr). Do not edit: change the docs and run npm run sync:docs. sha256:bc08714370ef437123354cae2af2e4423894ac1e87bd95fee2f6799b53725f8f -->
|
|
2
2
|
Canonical page: https://neon.com/docs/realtime/guides/ssr.md
|
|
3
3
|
|
|
4
4
|
> Summary: Server-render pages that use live queries. Read and seal the same query for each request, send both to the browser, render the initial rows straight away and let the first result bring the page live.
|
|
@@ -23,6 +23,8 @@ Render sealed queries and initial query results on the server.
|
|
|
23
23
|
- [Keep secrets on the server](#keep-secrets-on-the-server)
|
|
24
24
|
- [Examples](#examples)
|
|
25
25
|
|
|
26
|
+
Last updated: 2026-11-18
|
|
27
|
+
|
|
26
28
|
[Server-side rendering (SSR)](https://developer.mozilla.org/en-US/docs/Glossary/SSR) lets a page arrive with its rows already in the HTML, then subscribe to ongoing changes as soon as it hydrates.
|
|
27
29
|
|
|
28
30
|
Realtime supports SSR, including with [Next.js](https://neon.com/docs/realtime/examples/nextjs.md) and [TanStack Start](https://neon.com/docs/realtime/examples/tanstack.md).
|
|
@@ -559,7 +561,7 @@ export function getRouter() {
|
|
|
559
561
|
|
|
560
562
|
### Define the collection
|
|
561
563
|
|
|
562
|
-
Define the collection as a descriptor with the two-argument `collectionOptions(id, factory)` around [`realtimeCollectionOptions()`](https://neon.com/docs/realtime/sdks/typescript.md#tanstack-db-realtimecollectionoptions), and give it a stable id. Each `DbClient` runs the factory once, to create its own instance of the collection. Unlike `createMessagesCollection()` in the reactivity guide, it doesn't take a Realtime client: the factory reads the one from its `DbClient`. The descriptor is cheap to build, so the loader and the channel page each build it, and the id resolves both to the same collection:
|
|
564
|
+
Define the collection as a descriptor with the two-argument `collectionOptions(id, factory)` around [`realtimeCollectionOptions()`](https://neon.com/docs/realtime/sdks/typescript/tanstack-db.md#tanstack-db-realtimecollectionoptions), and give it a stable id. Each `DbClient` runs the factory once, to create its own instance of the collection. Unlike `createMessagesCollection()` in the reactivity guide, it doesn't take a Realtime client: the factory reads the one from its `DbClient`. The descriptor is cheap to build, so the loader and the channel page each build it, and the id resolves both to the same collection:
|
|
563
565
|
|
|
564
566
|
```ts filename="src/collections.ts"
|
|
565
567
|
import { collectionOptions } from "@tanstack/react-db"
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<!-- Generated by scripts/sync-skill-docs.mjs from website@
|
|
1
|
+
<!-- Generated by scripts/sync-skill-docs.mjs from website@HEAD:e520dd136+dirty (docs/realtime/guides/subscribing). Do not edit: change the docs and run npm run sync:docs. sha256:81179e0b1e9ab06abb855f26595bd639b5934dd3d3b9a7866270f06de5f934f3 -->
|
|
2
2
|
Canonical page: https://neon.com/docs/realtime/guides/subscribing.md
|
|
3
3
|
|
|
4
4
|
> Summary: Subscribe to live queries from trusted backend code or the browser. Fetch sealed queries from your API, handle every callback and renew sealed queries before they expire.
|
|
@@ -21,7 +21,9 @@ Subscribe to live queries using the Realtime client.
|
|
|
21
21
|
- [Log client diagnostics](#log-client-diagnostics)
|
|
22
22
|
- [Show full database errors](#show-full-database-errors)
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
Last updated: 2026-11-18
|
|
25
|
+
|
|
26
|
+
Once you've [defined](queries.md) and [sealed](queries.md#sealing-queries) a query, you can [subscribe to it](#subscribe-to-the-sealed-query) using the [low‑level Realtime client](https://neon.com/docs/realtime/sdks/typescript/client.md#client-createrealtimeclient).
|
|
25
27
|
|
|
26
28
|
**Important: Client-side reactivity**
|
|
27
29
|
|
|
@@ -39,7 +41,7 @@ The guide continues the [running example](https://github.com/neondatabase/exampl
|
|
|
39
41
|
|
|
40
42
|
### Subscribe in trusted backend code
|
|
41
43
|
|
|
42
|
-
Add a `url` to [`createRealtime()`](https://neon.com/docs/realtime/sdks/typescript.md#backend-createrealtime) and it returns a direct server, which seals queries with your secret and subscribes to them itself.
|
|
44
|
+
Add a `url` to [`createRealtime()`](https://neon.com/docs/realtime/sdks/typescript/backend.md#backend-createrealtime) and it returns a direct server, which seals queries with your secret and subscribes to them itself.
|
|
43
45
|
|
|
44
46
|
```ts filename="backend/worker.ts" {11,19,20,22}
|
|
45
47
|
import { createRealtime } from "@neon/realtime/server"
|
|
@@ -208,17 +210,17 @@ function renderMessage(message: Message): HTMLLIElement {
|
|
|
208
210
|
|
|
209
211
|
Rows arrive decoded, so `created_at` is a `Date`, as Drizzle's `Message` type says. The client's default parsers decode these Postgres types:
|
|
210
212
|
|
|
211
|
-
| Postgres type
|
|
212
|
-
|
|
|
213
|
-
| `bool`
|
|
214
|
-
| `int2`, `int4`, `oid`, `float4`, `float8`
|
|
215
|
-
| `json`, `jsonb`
|
|
216
|
-
| `timestamptz`
|
|
217
|
-
| `timestamp`
|
|
218
|
-
| `date`
|
|
219
|
-
| `bytea`
|
|
220
|
-
| `int8`, `numeric`
|
|
221
|
-
| Everything else, such as `text` and `uuid` | `string`, as Postgres formats it
|
|
213
|
+
| Postgres type | Arrives as |
|
|
214
|
+
| - | - |
|
|
215
|
+
| `bool` | `boolean` |
|
|
216
|
+
| `int2`, `int4`, `oid`, `float4`, `float8` | `number` |
|
|
217
|
+
| `json`, `jsonb` | The parsed JSON value |
|
|
218
|
+
| `timestamptz` | `Date` |
|
|
219
|
+
| `timestamp` | `Date` in local time |
|
|
220
|
+
| `date` | `Date` at local midnight |
|
|
221
|
+
| `bytea` | `Uint8Array` |
|
|
222
|
+
| `int8`, `numeric` | `string`, so their values stay exact |
|
|
223
|
+
| Everything else, such as `text` and `uuid` | `string`, as Postgres formats it |
|
|
222
224
|
|
|
223
225
|
`NULL` arrives as `null`, and arrays of built-in types decode element by element.
|
|
224
226
|
|
|
@@ -230,23 +232,23 @@ When you're done with a subscription, call `unsubscribe()`. It moves the subscri
|
|
|
230
232
|
|
|
231
233
|
A subscription has three callbacks. Each one takes a listener and returns a function that removes it:
|
|
232
234
|
|
|
233
|
-
| Callback
|
|
234
|
-
|
|
|
235
|
-
| `onChange`
|
|
236
|
-
| `onStateChange` | `state: LiveQueryState`
|
|
237
|
-
| `onUpdate`
|
|
235
|
+
| Callback | Listener arguments | Fires |
|
|
236
|
+
| - | - | - |
|
|
237
|
+
| `onChange` | `snapshot: LiveQuerySnapshot<Row>` | After each state change, after each `changes` update while `live`, and when a new result is complete. Materialized subscriptions only. |
|
|
238
|
+
| `onStateChange` | `state: LiveQueryState` | When the status changes. |
|
|
239
|
+
| `onUpdate` | `update: RawLiveQueryUpdate<Row>` | With `type: "rows"` and `rows`, each `{ rowId, row }`, when a complete, authoritative result replaces the previous one. With `type: "changes"`, `changes` and `batch: LiveQueryBatchInfo` when committed transactions change the result. Each change is an `upsert` with a `rowId` and `row`, or a `remove` with only a `rowId`. |
|
|
238
240
|
|
|
239
241
|
Listeners run in the order you add them, and one that throws doesn't stop the others. Each initial result and each committed transaction becomes visible atomically. Outside a listener, `getSnapshot()` and `getState()` return the current values.
|
|
240
242
|
|
|
241
243
|
The status tells you how far to trust the rows:
|
|
242
244
|
|
|
243
|
-
| Status
|
|
244
|
-
|
|
|
245
|
+
| Status | Meaning |
|
|
246
|
+
| - | - |
|
|
245
247
|
| `connecting` | Waiting for a result: when it starts, including with `initialData` from a [server render](ssr.md), after a reconnect, and after renewing an expired sealed query. Rows you already have stay. |
|
|
246
|
-
| `live`
|
|
247
|
-
| `stale`
|
|
248
|
-
| `error`
|
|
249
|
-
| `closed`
|
|
248
|
+
| `live` | The rows are authoritative and changes are streaming. It stays `live` while the server replaces the whole result. |
|
|
249
|
+
| `stale` | The sealed query expired and the subscription is waiting for a renewed one. The rows are usable but may be out of date. Render them normally. |
|
|
250
|
+
| `error` | Automatic recovery has stopped. See [handle errors](#handle-errors). |
|
|
251
|
+
| `closed` | Closed by `unsubscribe()` or `client.close()`. It doesn't reopen. |
|
|
250
252
|
|
|
251
253
|
By default, subscriptions are materialized. The client keeps the rows and applies each update before `onUpdate` fires, so `getSnapshot()` in an update listener already includes it. If your code keeps its own rows, as TanStack DB does, pass `{ materialize: false }` for a raw subscription. It has the same callbacks except `onChange`, and no `getSnapshot()`. Keep the rows in a map keyed by `rowId`, replace them on each `rows` update and apply each `changes` update's upserts and removes:
|
|
252
254
|
|
|
@@ -276,7 +278,7 @@ subscription.onUpdate((update: RawLiveQueryUpdate<Message>) => {
|
|
|
276
278
|
for (const change of update.changes) {
|
|
277
279
|
if (change.type === "upsert") {
|
|
278
280
|
rows.set(change.rowId, change.row)
|
|
279
|
-
} else {
|
|
281
|
+
} else if (change.type === "remove") {
|
|
280
282
|
rows.delete(change.rowId)
|
|
281
283
|
}
|
|
282
284
|
}
|
|
@@ -291,7 +293,7 @@ subscription.onStateChange((state: LiveQueryState) => {
|
|
|
291
293
|
|
|
292
294
|
A `rowId` is opaque and scoped to its subscription, and each `rows` update invalidates every earlier one. It isn't a primary key, so don't store it or match it against your own ids. A `rows` update can arrive at any time, for example after a reconnect, and it always replaces the whole result.
|
|
293
295
|
|
|
294
|
-
Each `changes` update also carries the ids of the Postgres transactions it covers, in `update.batch.txids`. The [mutations guide](mutations.md) uses them to confirm optimistic mutations.
|
|
296
|
+
Each `changes` update also carries the ids of the Postgres transactions it covers, in `update.batch.txids`. The [mutations guide](mutations.md) uses them to confirm optimistic mutations. With `ORDER BY`, `changes` can also include `move` changes, which carry a `rowId` and the `afterRowId` it now follows (`null` for the first row), and no row.
|
|
295
297
|
|
|
296
298
|
## Renew the sealed query
|
|
297
299
|
|
|
@@ -415,11 +417,11 @@ function renderMessage(message: Message): HTMLLIElement {
|
|
|
415
417
|
|
|
416
418
|
If the replacement's `queryFingerprint` doesn't match, the promise that `renew()` returns rejects. A changed parameter, like a different channel, makes a different query, so subscribe to the new one and unsubscribe from the old.
|
|
417
419
|
|
|
418
|
-
You don't write this yourself when you use the bindings. The [React and TanStack DB bindings](reactivity.md) take a `refreshQuery` function and renew with the same timing, 10 seconds before expiry with a 1-second retry. They're built on [`QueryRefreshController`](https://neon.com/docs/realtime/sdks/typescript.md#client-queryrefreshcontroller), which you can use if you're writing a binding for another framework.
|
|
420
|
+
You don't write this yourself when you use the bindings. The [React and TanStack DB bindings](reactivity.md) take a `refreshQuery` function and renew with the same timing, 10 seconds before expiry with a 1-second retry. They're built on [`QueryRefreshController`](https://neon.com/docs/realtime/sdks/typescript/client.md#client-queryrefreshcontroller), which you can use if you're writing a binding for another framework.
|
|
419
421
|
|
|
420
422
|
## Handle errors
|
|
421
423
|
|
|
422
|
-
When automatic recovery stops, the subscription moves to `error`, and its `error` is a [`LiveQueryError`](https://neon.com/docs/realtime/sdks/typescript.md#client-livequeryerror) with:
|
|
424
|
+
When automatic recovery stops, the subscription moves to `error`, and its `error` is a [`LiveQueryError`](https://neon.com/docs/realtime/sdks/typescript/client.md#client-livequeryerror) with:
|
|
423
425
|
|
|
424
426
|
- `code`: a stable, machine-readable error code
|
|
425
427
|
- `message`: a description of the failure
|
|
@@ -437,7 +439,7 @@ If the connection itself fails, for example because of a fatal protocol error, e
|
|
|
437
439
|
|
|
438
440
|
### Log client diagnostics
|
|
439
441
|
|
|
440
|
-
The client is silent by default. To see what it's doing, pass a `logLevel` to [`createRealtimeClient()`](https://neon.com/docs/realtime/sdks/typescript.md#client-createrealtimeclient): `"error"`, `"warn"`, `"info"` or `"debug"`. Each level includes the ones before it.
|
|
442
|
+
The client is silent by default. To see what it's doing, pass a `logLevel` to [`createRealtimeClient()`](https://neon.com/docs/realtime/sdks/typescript/client.md#client-createrealtimeclient): `"error"`, `"warn"`, `"info"` or `"debug"`. Each level includes the ones before it.
|
|
441
443
|
|
|
442
444
|
```ts filename="frontend/client.ts" {7}
|
|
443
445
|
import { createRealtimeClient } from "@neon/realtime/client"
|
|
@@ -450,7 +452,7 @@ export const client = createRealtimeClient({
|
|
|
450
452
|
})
|
|
451
453
|
```
|
|
452
454
|
|
|
453
|
-
Entries go to the console unless you pass a [`logger`](https://neon.com/docs/realtime/sdks/typescript.md#client-realtimelogger), which receives each [entry](https://neon.com/docs/realtime/sdks/typescript.md#client-realtimelogentry) instead. An entry has a `level`, a `message`, a `timestamp` and a stable `event` name, like `connection_lost` or `subscription_failed`. Match on `event`, not on `message`:
|
|
455
|
+
Entries go to the console unless you pass a [`logger`](https://neon.com/docs/realtime/sdks/typescript/client.md#client-realtimelogger), which receives each [entry](https://neon.com/docs/realtime/sdks/typescript/client.md#client-realtimelogentry) instead. An entry has a `level`, a `message`, a `timestamp` and a stable `event` name, like `connection_lost` or `subscription_failed`. Match on `event`, not on `message`:
|
|
454
456
|
|
|
455
457
|
```ts
|
|
456
458
|
logger: (entry) => {
|
|
@@ -480,6 +482,8 @@ export const realtime = createRealtime({
|
|
|
480
482
|
|
|
481
483
|
Every query it seals then reports errors with the database's full message, plus its SQLSTATE code in `sqlState` when there is one. It's a backend option because it travels inside each sealed query.
|
|
482
484
|
|
|
485
|
+
To check a query's full error from the terminal without writing code, run [`neon realtime subscribe --debug`](https://neon.com/docs/cli/realtime.md#subscribe).
|
|
486
|
+
|
|
483
487
|
**Warning: Development only**
|
|
484
488
|
|
|
485
489
|
Full error details can expose schema, table and column names to anyone who subscribes, including through any error message your UI shows. Never turn on `debugMode` in production.
|