Skip to content

Subscribe to real-time changes

Block agents on db.subscribe() instead of polling. The DO pushes table-change events the moment a write lands, waking the agent only when state it cares about changes.

import { PerSQL } from "@persql/sdk";
const persql = new PerSQL({ token: process.env.PERSQL_TOKEN! });
const db = persql.database("acme/agent-1");
await db.query(`
CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY,
status TEXT,
payload TEXT
)
`);
const off = db.subscribe({
tables: ["tasks"],
onChange: (e) => console.log(e.kind, e.table),
});
await db.query("INSERT INTO tasks (status, payload) VALUES (?, ?)", [
"pending",
JSON.stringify({ task: "summarize" }),
]);
// The agent wakes when the row lands.
off();

Because every database is its own Durable Object, events are implicitly scoped to exactly one agent with no routing layer.

Per-agent sandbox