<!--
Sitemap:
- [rxfy](/index): Typed, normalized, reactive state — built on RxJS
- [Comparison](/comparison): rxfy versus Redux Toolkit, MobX, Jotai, TanStack Query, and TanStack DB
- [Inspired by](/inspired-by): the libraries and ideas rxfy grew out of
- [Agent Skills](/agent-skills): Accurate rxfy context for AI coding assistants
- [Examples](/examples): Runnable apps, from client-only to fully synced
- [Changelog](/changelog)
- [Getting Started](/getting-started)
- [Create Store](/getting-started/create-store): Normalized reactive state in a client-only app
- [Add SSR](/getting-started/add-ssr): Render the first paint on the server, hydrate with no refetch
- [Add Sync Client](/getting-started/add-sync-client): The full stack: the server publishes, the client syncs
- [Core Concepts](/core-concepts): The ideas rxfy is built on
- [Observables](/core-concepts/observables): A value that changes over time, that you can subscribe to
- [Normalization](/core-concepts/normalization): Store each entity once, reference it by id
- [Late Unwrapping](/core-concepts/late-unwrapping): Unwrap async state at the leaf, not the trunk
- [Server-Side Rendering](/core-concepts/ssr): Dehydrate on the server, hydrate with no refetch
- [rxfy](/rxfy): The core package: atoms, lenses, models, and states
- [createModel](/rxfy/create-model): Typed entities in normalized storage
- [defineState](/rxfy/define-state): Typed, normalized state shapes with fetching and mutations
- [createAtom](/rxfy/create-atom): A reactive cell with synchronous get, set, and modify
- [createLens](/rxfy/create-lens): A two-way view into part of an Atom
- [React Bindings](/react): Hooks and helpers for using rxfy in React
- [useStateData](/react/use-state-data): Fetch, normalize, and subscribe to a query
- [useStatePagedData](/react/use-state-paged-data): Paginated and infinite-scroll lists
- [useModelStore](/react/use-model-store): Subscribe to one normalized entity by id
- [useAtom](/react/use-atom): Two-way binding for any IAtom
- [Pending](/react/pending): Render pending, rejected, and fulfilled UI for any observable
- [usePending](/react/use-pending): The status value behind Pending
- [useObservable](/react/use-observable): Bind a raw Observable to React
- [Sync Client in React](/react/sync-client): StoreProvider, useSyncClient, and update handles
- [rxfy-client](/framework/client): The framework-agnostic browser sync runtime
- [createSyncClient](/framework/client/create-sync-client): Connect a transport and drive the sync loop
- [readSsrGrants](/framework/client/read-ssr-grants): Lift SSR-embedded channel grants
- [rxfy-server](/framework/server): Bind Drizzle tables, write, and publish sync updates
- [defineResource](/framework/server/define-resource): Tie a Drizzle table to an rxfy model
- [createSync](/framework/server/create-server): Wire a storage adapter, hub, and secret into a Live object
- [createInMemoryHub](/framework/server/hub): The socket-keyed pub/sub backbone
- [Writes](/framework/server/writes): sync.create / sync.update / sync.delete and touch
- [Storage adapters](/framework/server/storage-adapters): Persist writes with Drizzle or in memory
- [Sync messages](/framework/server/messages): What travels between server and client
- [Grants](/framework/server/grants): The server signs what it serves; the client subscribes with the token
- [rxfy-ws](/framework/ws): The default WebSocket transport
- [createWsServer](/framework/ws/server): Attach a Hub to WebSocket connections
- [createWsClient](/framework/ws/client): The browser transport with reconnect and replay
- [Custom transports](/framework/ws/custom-transport): Bring your own ClientTransport
- [Guides](/guides): Task-focused walkthroughs of common rxfy patterns
- [Pagination and infinite scroll](/guides/pagination): Load and append pages into one normalized list
-->

# createSync \[Wire a storage adapter, hub, and secret into a Live object]

`createSync` wires together a storage adapter, hub, and signing secret into a
`Live` object with typed write methods plus the grant calls `serve`, `renew`, and `hydration`.

`storage` is a [`SyncStorage` adapter](/framework/server/storage-adapters) — `drizzleStorage(db)` from
`rxfy-server-drizzle` for a Drizzle-backed app, or `memoryStorage()` from `rxfy-server-memory` for an
in-memory one — and
`secret` is the HMAC key used to sign and verify [channel grants](/framework/server/grants) —
share it with the WebSocket server. The writers (`create` / `update` / `delete`) delegate to the
adapter, so core `rxfy-server` never imports Drizzle. The tabs below show them:

:::code-group

```ts [server/live.ts]
import { createInMemoryHub, createSync } from "rxfy-server";
import { drizzleStorage } from "rxfy-server-drizzle";
import { db } from "./db.js";

export const sync = createSync({
  storage: drizzleStorage(db),

  // The hub holds sync socket subscriptions, so there must be exactly ONE instance. entry-server
  // receives `live` as a parameter instead of importing this module, so a separate Vite SSR module
  // graph never instantiates a second hub.
  hub: createInMemoryHub(),

  // Signs/verifies channel grants — shared with the WebSocket server. Override via RXFY_SECRET.
  secret: process.env.RXFY_SECRET ?? "dev-secret-change-me",
});
```

```ts [src/blog/resources.ts]
import { postModel, userModel } from "./models.js";
import { defineResource } from "rxfy-server-drizzle";
import { posts, users } from "../db/schema.js";

export const userResource = defineResource({ table: users, model: userModel });
export const postResource = defineResource({ table: posts, model: postModel });
```

```ts [server/db.ts]
import { PGlite } from "@electric-sql/pglite";
import { drizzle } from "drizzle-orm/pglite";
import { posts, users } from "../src/db/schema.js";

// Any Drizzle database works — this example uses in-memory PGlite.
export const db = drizzle(new PGlite());

// Re-exported so server routes can import tables and `db` from one place.
export { posts, users };
```

:::

The returned `Live` object exposes:

* [`sync.create` / `sync.update` / `sync.delete` / `sync.touch`](/framework/server/writes) —
  typed writes that publish sync messages.
* [`sync.serve(state, params, data)`](/framework/server/grants) — read-endpoint wrapper; parses
  `data` (the state's input shape — raw DB rows work as-is) through the state's schemas, signs a
  grant for the state's channel, and returns the parsed shape with the grant attached as `$grant`.
  Stateless — no `req`, no hub interaction.
* [`sync.renew(grant)`](/framework/server/grants) — verify a grant (within a grace window) and
  reissue it with a fresh expiry, or return `null` when denied. Mount it behind your app's auth.
* [`sync.hydration(registry)`](/framework/server/grants) — one-call SSR payload: signs one grant
  per channel the render logged, embeds them as `grants: string[]`, and returns the
  `hydrationScript` string.
  `createSync` also accepts `grantTtlMs` (grant lifetime, default 15 minutes) and `renewGraceMs`
  (how long past expiry a grant still renews, default 5 minutes). `Live` is generic over the
  adapter's binding — `Live<DrizzleBinding>` for `drizzleStorage`, `Live<MemoryBinding>` for
  `memoryStorage`.

Its collaborators each have their own article: resources come from
[`defineResource`](/framework/server/define-resource) (Drizzle) or `defineCollection`
(`rxfy-server-memory`), and the hub from
[`createInMemoryHub`](/framework/server/hub) — it holds per-socket subscriptions, so there must be
exactly one instance per process.
