matter.work
Building Apps / The SDK

Live reads

Subscribe to queries and counts that update immediately whenever matching rows change.

Every read method has a live counterpart that delivers the current result immediately and fires again whenever matching rows change—without polling or waking on unrelated writes. Boards have the same live reads for tasks; see Boards and tasks.

Watching a query
const deals = matter.table('deals')

const stop = deals.watch({ index: 'by_stage', eq: { stage: 'lead' }, limit: 50 }, (state) => {
  if (state.status === 'ready') renderDeals(state.value.rows)
  else if (state.status === 'error') renderError(state.error.message)
})

// Call stop() when the component unmounts:
stop()

Live watch methods#

Method state.value when ready
watch(read, listener) { rows, nextCursor, prevCursor } — same payload as query(read).
watchCount(read, listener) number — live count of matching rows.
watchExists(read, listener) boolean — whether at least one matching row exists.
watchGroupCount(read, listener) { groups, more } — live counts per group.
watchIds(read, listener) { ids, nextCursor } — live list of matching row IDs.
watchRows(ids, listener) { rows } — watches specific rows by ID and fires whenever any of them changes.

Listener state lifecycle#

Your listener receives:

  • { status: 'loading' } synchronously on registration.
  • { status: 'ready', value, source, cached? } as soon as the first answer arrives and after every subsequent change. source is 'local' (under 10,000 rows) or 'server' (large tables), and cached: true indicates a cached snapshot shown while a server query refreshes.
  • { status: 'error', error: { code, message } } if the query fails.

All live reads across a page or Node process multiplex over a single shared event stream that automatically reconnects and re-subscribes if interrupted. Optimistic writes you make on the same computer show up in your live reads immediately, before the server round-trip finishes.

Watching in background workers and React#

In a background worker, use watchIds to react as soon as a row enters a target state (such as status: 'approved'):

src/worker.ts
matter
  .table('actions')
  .watchIds(
    { index: 'by_sender_status', eq: { sender: '$viewer', status: ['approved', 'sending'] }, limit: 500 },
    (state) => {
      if (state.status === 'ready') void processApproved(state.value.ids)
    },
  )

In React, wrap watch in a small hook typed with TableRead:

src/useQuery.ts
import { useEffect, useState } from 'react'
import matter, { type Row, type Table, type TableRead, type Tables } from '@matter.work/app-sdk'

type TableName = keyof Tables & string

export function useQuery<N extends TableName>(table: N, read: TableRead<Tables[N]> | null) {
  const [rows, setRows] = useState<Row<Tables[N]['values']>[]>([])
  const key = JSON.stringify(read)
  useEffect(() => {
    if (!read) return
    const untyped = matter.table(table) as unknown as Table
    return untyped.watch(read as never, (state) => {
      if (state.status === 'ready') setRows(state.value.rows as Row<Tables[N]['values']>[])
    })
  }, [table, key])
  return rows
}