GitHub 37
Skip to content
ViteHub is still experimental. Expect bugs and breaking changes.

Sync

Say which rows a person may see. The browser keeps a copy of them, writes at once, and syncs when the network is there.

Deploys on
  • Node
  • Docker
  • Cloudflare
  • Vercel
  • Netlify
server/sync/todos.ts
import { defineSync } from 'vite-hub/sync'
import { todos } from '../databases/default'

// One file is one synced table, at /_vitehub/sync/todos
export default defineSync({
  table: todos,
  where: ({ user }) => user ? { ownerId: user.id } : false,
})
server/sync/todos.ts
PartWhat it does
Local copyThe browser caches rows in IndexedDB. Reloading renders cached rows right away, then pulls what changed.
Writes at onceinsert, update, and delete update rows immediately. Each write goes to a queue in IndexedDB before it is sent, so it survives offline time, a reload, and a crash.
OrderSync sends one push at a time in the order you made the edits, so sequential updates to a row apply in order.
Scopewhere returns the rows that the caller can see, such as { ownerId: user.id } or the caller's teams, { teamId: ['design', 'web'] }. The server filters reads with it, writes its values into inserts, and refuses writes outside it.
Moved rowsA deleted row or a row moved to another team leaves devices on the old team and syncs only to the new team.
SubsetsuseSync('comments', { where: { issueId } }) syncs a part of the rows, with its own local copy, on the columns that subsets lists. A second visit shows it from the device.
ActionsNamed writes with server logic, such as cards.actions.rate({ id, stars }), share the same queue and change log as row writes. Call refuse(message) to reject one.
LiveEach tab keeps one live stream open for all synced tables so changes from others arrive within about a second.
Account guardEach write records the account that made it. If another person signs in, old writes wait until their account signs back in. Apps with custom auth return the user from identify.
Once onlyEach write has an ID. When a response is lost and the client sends the write again, the server does not apply it twice.
Fresh rowsAlongside the live stream, the client pulls on start, when the page shows again, when the network comes back, and after each write. Other tabs update at once. When the table changes, the browser drops cached rows and keeps queued writes.
InspectionInspect tables and changes via vitehub sync list, vitehub sync changes <name>, and the sync.list and sync.changes operations.

The client needs no framework. Vue uses useSync(), and other frameworks use the same store through createSync().

Use Sync when each person or team works on their own rows in the browser. Use Database when only server code reads and writes the table. Use Realtime when people send short messages or show who is online in a room.

  • Get started builds an offline-ready todo list.
  • Server API covers defineSync(), team scopes, subsets, actions, live pulls, and CLI commands.
  • Limits and errors lists what Sync does not do and the SYNC_* codes.
  • Vue covers useSync(), error handling, and sign-out.