Live updates
A resource with .live() keeps the pages people have open on its records current: when a record changes, every open list, record page, count and chart of the resource reads it again within a second or two, without a reload. The changes are captured by the same database triggers as events, so writes from psql, other services and cron SQL show up too, not only those through the API.
import { f, resource } from '@protobase/schema'
// A team's tasks: a list or task page someone has open shows a colleague's change within a second or two.
/** tasks */export const tasks = resource('tasks') .table('tasks') .fields({ id: f.integer().readOnly().filterable().sortable(), title: f.text(), status: f.enum(['todo', 'doing', 'done']).default('todo').filterable(), }) .primaryKey((r) => r.id) .live()What a page hears
Section titled “What a page hears”The server tells each open app which records changed, by resource and key, never their values. The app then reads again what it shows of that resource through the regular API, as its user, so the same tenant, row filters and field rules apply as to any other read. A page only hears of the live resources its user may list or read, in their tenant.
Changes that come close together are read once: a page gathers them for 300 milliseconds. A form with unsaved changes keeps the record it was opened with, so its Save still finds a conflict when someone else changed the record; the record is read again once the form is saved or left.
It is live data, not multiplayer editing: two people editing the same record still see a conflict on Save, as without .live().
The stream
Section titled “The stream”GET /api/live answers a text/event-stream to anyone who may read a live resource, a visitor of the anonymous role too. Each event’s data is JSON with a cursor:
type |
|
|---|---|
current |
Nothing new up to cursor. Sent first, and every 25 seconds. |
changes |
changes: [{ resource, key, operation }] in commit order, operation being create, update, delete or undelete. |
resync |
Changes went by that are no longer kept: read everything shown again. |
Connecting again with ?after=<cursor> catches up on what was missed while disconnected. client.live({ onChanges, onResync }) of @protobase/client follows the stream and connects again by itself, as the app does. However many callers of one client follow it, they share one stream, so a page with many live views holds one connection, which matters under HTTP/1.1, where a browser opens at most six to a host.
Resources backed by a view
Section titled “Resources backed by a view”A resource backed by a database view, such as a chat thread that merges messages and replies, updates live when the tables it reads change. A view takes no trigger, so name the resources it reads with .live({ reads }): a change in any of them refreshes the view’s open lists, record pages and related lists, and a change in another table does not.
import { f, resource } from '@protobase/schema'
// A chat thread over a view merging messages and replies: an open thread shows a new message or reply within a second or two.
/** thread */export const thread = resource('thread') .table('thread') .fields({ id: f.text().filterable(), body: f.text(), sent_at: f.timestamp().sortable(), }) .primaryKey((r) => r.id) .live({ reads: ['messages', 'replies'] })Each resource it names is captured like a live one, needs no .live() of its own and still needs a resource of its own; protobase dev and protobase serve do not start when a name is not a resource, or is another resource backed by a view: name the tables that view reads instead. The stream sends the view a change with an empty key, { resource, key: '', operation: 'update' }, once for writes that come close together: any of its records may have changed, so the page reads what it shows of the view again. A reader hears of it in the tenant of the record that changed, or in every tenant when that resource has no tenant.
Polling, then a socket
Section titled “Polling, then a socket”pollThenSocket({ poll, onError, socket, onMessage, pollMs?, switchAfterMs?, socketFirst? }) of @protobase/client, in the browser and on Node, keeps up with anything that changes, until the function it returns is called. It calls poll at once and again pollMs (5 seconds) after each call ends, also after one that rejected, whose error goes to onError, and after switchAfterMs (30 seconds) opens the WebSocket socket() returns, such as () => new WebSocket(url). Once the socket is open, polling stops: poll runs once more, for what changed before the socket opened, and onMessage gets each message’s data. When the socket closes, or never opens, it polls again at once and opens a new socket switchAfterMs later. Polling goes on while a socket connects, so nothing waits on it. A short visit thus never opens a socket; socketFirst: true opens one at once instead, for a process that stays, and polls only while it is down.
Who is on a record’s page and what they send each other, which no change of a record says, goes over the record’s channel.
The table
Section titled “The table”protobase migration adds the table changes.notices to the changes schema of the event capture (renamed with events: { schema }), and the capture triggers of each live resource and of each resource a live view reads; apply it with the project’s other migrations. protobase dev and protobase serve do not start while the table or a trigger is missing.
A notice is a resource, a record key, the record’s tenant and the operation, written by the changes.dispatch job in the transaction that removes the change from the outbox; a live resource therefore needs the jobs schema and the app’s workers. Notices are kept for 10 minutes. Each serving process with at least one page open reads the table once a second, however many pages it serves, so live updates work with several serving processes, with PROTOBASE_JOBS=false and through a pooler in transaction mode.