# Local-First Sync

> Opt into local-first replication with one line — optimistic hooks, a changefeed, and a transaction queue.

A resource opts into local-first replication with `sync: { mode: "local", … }` in its options. The client runtime — an IndexedDB store, an optimistic transaction queue, and a long-poll changefeed — is generated from the `x-rastack-sync` OpenAPI extension.

```ts title="resources/chat.ts"
export const Message = resource("chat", "message", {
  room: s.ref(() => Room),
  body: s.string(),
}, {
  sync: { mode: "local" },
});
```

## Optimistic by default

Synced hooks are async and optimistic by default: a mutation applies to the local store immediately and reconciles with the server in the background. Individual operations can opt into blocking ("sync") semantics where correctness demands it — for example, a password change.

## The client runtime

| Piece | Role |
| --- | --- |
| `tools/sync/` store | an IndexedDB-backed local copy of each synced table |
| transaction queue | holds optimistic mutations until the server confirms them |
| long-poll changefeed | streams server changes back into the local store |

> **Warning:** The client runtime ships today. The server endpoints (`/api/sync/v1/{bootstrap,changes}`) are being ported to the Rust API; `rastack compile` already emits `x-rastack-sync` so the client hooks generate now.

---

_Source: [https://reactapistack.com/docs/sync](https://reactapistack.com/docs/sync) · Full docs as one file: [llms-full.txt](https://reactapistack.com/llms-full.txt)_
