Skip to content

Server-side clients

You can also use LiveStore on the server side e.g. via the @livestore/adapter-node adapter. This allows you to:

  • have an up-to-date server-side SQLite database (read model)
  • react to events / state changes on the server side (e.g. to send emails/push notifications)
  • commit events on the server side (e.g. for sensitive/trusted operations)

Note about the schema: While the events schema needs to be shared across all clients, the state schema can be different for each client (e.g. to allow for a different SQLite table design on the server side).

import { makeAdapter } from '@livestore/adapter-node'
import { createStorePromise } from '@livestore/livestore'
import { makeCfSync } from '@livestore/sync-cf'
import { events, schema, tables } from './livestore/schema.js'
const adapter = makeAdapter({
storage: { type: 'fs', baseDirectory: 'tmp' },
sync: { backend: makeCfSync({ url: 'ws://localhost:8787' }), onSyncError: 'shutdown' },
})
const store = await createStorePromise({
adapter,
schema,
storeId: 'test',
syncPayload: { authToken: 'insecure-token-change-me' },
})
const todos = store.query(tables.todos.where({ completed: false }))
  • The @livestore/adapter-node adapter doesn’t yet work with Cloudflare Workers but you can follow this issue for a Cloudflare adapter to enable this use case.
  • Having a @livestore/adapter-cf-worker adapter could enable serverless server-side client scenarios.