This page shows how to build a Realtime app with Kysely, using your existing schema, queries, types and writes.
You'll learn how to add the Kysely adapter, define your schema, migrations and live queries, pass query parameters, keep end-to-end type-safety and return the transaction ID from Kysely writes.
Full example on GitHub
The realtime/with-kysely app in the neondatabase/examples repository is a framework-free TypeScript app built with Kysely and Realtime.
Setup
Install the packages
Install the Realtime SDK, its Kysely adapter, Kysely and the pg driver:
npm install @neon/realtime @neon/realtime-kysely kysely pg
npm install -D @types/pg@neon/realtime-kysely exports kyselyAdapter() to use in your backend. There are no client imports. The queries guide's setup covers neon.ts and the environment variables. For Kysely on Neon in general, see Connect from Kysely to Neon.
Create the Realtime server
Pass kyselyAdapter() to createRealtime, so the Realtime server accepts Kysely queries:
import { createRealtime } from "@neon/realtime/server"
import { kyselyAdapter } from "@neon/realtime-kysely"
const realtime = createRealtime({
secret: env.realtime.secret,
db: postgresDatabaseName,
adapter: kyselyAdapter() // seals Kysely query builders, not raw SQL
})kyselyAdapter()takes no options. When you seal a Kysely select, it compiles it to SQL and bind parametersdbis the database name, not a Kysely instance. The example derives it fromDATABASE_URLwith@neon/env(aspostgresDatabaseNameinsrc/env.ts), reads the secret from the same parsed env, and keepsNEON_REALTIME_SECRETon the server- the same server still seals
rawSqlqueries, so you can mix the two
Defining queries
Define live queries with your usual Kysely schema, instance and select builder, in server code.
Define the schema
Your Database interface stays plain Kysely. Use the column names as its keys:
import type { Generated, Selectable } from "kysely"
// Kysely's types for the table. src/setup.ts creates it.
// Keys equal the column names: Realtime builds live rows from PostgreSQL's result names, so the
// live rows and the rows the server reads share the same keys.
export interface TodosTable {
id: Generated<number> // SERIAL PRIMARY KEY
title: string
completed: Generated<boolean> // defaults to false
}
export interface Database {
realtime_todos: TodosTable
}
// The row the live query returns. Kysely infers it from the table instead of a handwritten
// interface, keeping the `{ id, title, completed }` shape that api-types.ts and the client import.
export type Todo = Selectable<Database["realtime_todos"]>Realtime names each field in a live row after the column name Postgres returns, so keys that match the columns keep live rows and Kysely's types in step. That's why CamelCasePlugin doesn't work.
The example creates the table once with Kysely's schema builder in src/setup.ts, run with npm run db:setup. You could also use a Kysely migration or your usual migration tool:
await db.schema
.createTable("realtime_todos")
.ifNotExists()
.addColumn("id", "serial", (column) => column.primaryKey())
.addColumn("title", "text", (column) => column.notNull())
.addColumn("completed", "boolean", (column) =>
column.notNull().defaultTo(false)
)
.execute()
// Seed one row only when the table is empty, so re-running stays idempotent.
const existing = await db
.selectFrom("realtime_todos")
.select("id")
.limit(1)
.executeTakeFirst()
if (!existing) {
await db
.insertInto("realtime_todos")
.values({ title: "Open this app in another tab" })
.execute()
}The primary key is all Realtime needs to work. See the table requirements for more details.
Create the Kysely instance
The example keeps the pg pool in src/db.ts:
import { Pool } from "pg"
import { env } from "./env.js"
export const pool = new Pool({
connectionString: env.postgres.databaseUrl,
max: 5,
min: 1
})
pool.on("error", (error) => {
console.error("Unexpected error on idle PostgreSQL client", error)
})src/server.ts and src/setup.ts build a Kysely instance over that pool. The adapter compiles your queries using Kysely's PostgreSQL compiler, whatever your dialect. db still runs your writes and any reads on the server, such as server-rendered rows:
import { Kysely, PostgresDialect } from "kysely"
import { pool } from "./db.js"
import type { Database } from "./schema.js"
const db = new Kysely<Database>({
dialect: new PostgresDialect({ pool })
})Define the live query
A live query is a Kysely select that you build but don't execute. Wrap it in a function, so each call builds a fresh query:
// The only place the live query is built, so the sealed query matches the rows the server reads.
const selectTodos = () =>
db
.selectFrom("realtime_todos")
.select(["id", "title", "completed"])
.orderBy("id")- this example's list is shared, so the query has no
whereclause. To scope a live query so each user only sees their own rows, add awhereon context you trust; that clause is the authorization. See sealing authorized queries - call
.execute()on the same builder to read the rows on the server, for example to server-render the first results. See return the sealed query and initial results - rename a field with an alias, as in
"id as todoId", and name a computed field with.as() Todois the row type, whichsrc/schema.tsinfers from the table. You'll use it in the browser
The adapter doesn't check the SQL. That happens when a client subscribes, so a query that calls a non-deterministic function such as now() seals and then fails to subscribe. See the rules for defining live queries.
Sealing queries
Passing query parameters
This example seals a single query with no parameters. When a query does need a parameter (like a user ID or search term) Kysely compiles each value to a bind parameter. The adapter encodes values the way node-postgres does, and Postgres infers each parameter's type from the SQL.
Where Postgres can't infer a parameter's type from the SQL, wrap the value in a pgParam helper for a built-in type, inside a sql template.
Subscribing to queries
Subscribe in the browser as the subscribing guide shows. The client needs no Kysely-specific parsers: its defaults decode like node-postgres, which PostgresDialect uses, so each live row is the same { id, title, completed } — a number, a string and a boolean — that Kysely reads on the server.
seal() infers the row type from the select: realtime.seal({ query: selectTodos() }) resolves to a SealedLiveQuery<Todo>.
- to name the row type in the browser, use
import type { Todo } from "../../src/schema.js". TypeScript erases the import, so Kysely and your server code stay out of the browser bundle - if your dialect runs on another driver, pass its parsers, such as
postgresJsParsers. For custom types, register a parser withdefineParsers - for the rest, see subscribing to the sealed query and the React and TanStack DB bindings
Optimistic mutations
The client holds each optimistic change until the write's transaction ID comes back on the subscription, so every write returns its txid. The mutations guide explains how optimistic mutations work.
Return the txid from Kysely writes
Return the transaction ID from the write's own statement, with .returning(). Every mutation in the example — insert, toggle and delete — returns it. The example is a plain Node HTTP server; here is its toggle handler:
import { sql } from "kysely"
// Same statement on every mutation: the client waits for this txid over Realtime before dropping
// its optimistic state, so the write must keep returning it.
const txid = sql<string>`pg_current_xact_id()::text`.as("txid")
// ...inside the request handler, for PATCH /api/todos/:id
const result = await db
.updateTable("realtime_todos")
.set({ completed: sql<boolean>`NOT completed` })
.where("id", "=", Number(match[1]))
.returning(txid)
.executeTakeFirst()
return result
? json(response, 200, result satisfies MutationResponse)
: json(response, 404, { error: "Todo not found" })pg_current_xact_id()runs in the write's statement, so it returns the ID of the transaction that made the change, with any driver. Thetextcast keeps the 64-bit ID exact, as the decimal string the client waits forexecuteTakeFirst()returnsundefinedwhen no row matched. Nothing comes over Realtime, so the handler responds404and the client has nothing to wait for- the insert returns the new
idtoo, with.returning(["id", txid]), so the client can swap its optimistic row for the real one. This demo doesn't authenticate its routes — add authorization before adapting it for production - for writes that span several statements, run them all on
trxindb.transaction().execute(async (trx) => ...), and read the txid there withtrx.selectNoFrom(txid).executeTakeFirstOrThrow() - call it from a route or server function, and see how the frontend calls these writes
Common pitfalls
These don't work as live queries:
CamelCasePlugin. Its result transform doesn't apply to live rows, so they keep the snake_case column names while Kysely types camelCase keys. Aliases don't help, because the plugin snake-cases them too. Remove the plugin and use the column names as keys. For a camelCase field, alias the column, as in"created_at as createdAt"- anything but a select builder. TypeScript rejects an executed result, a compiled query, an insert, update or delete builder, and a bare
sql`...`template. Pass the builder fromselectFrom(), and seal raw SQL withrawSql
Next steps
- read the queries guide
- see how the client applies writes optimistically in the mutations guide
- look up
kyselyAdapter()in the Kysely section of the SDK reference - browse the full
realtime/with-kyselyexample on GitHub
Need help?
Join our Discord Server to ask questions or see what others are doing with Neon. For paid plan support options, see Support.








