Build realtime HTML pages
Keep an HTML page current when tables change, and sync view state across a person's tabs.
React to table changes
When a table the page has read with think.executeSql changes, whoever changed it, the page receives a think:data event. Query again to stay current; the page itself is not reloaded.
async function load() {
const { rows } = await think.executeSql('SELECT * FROM "Tasks" WHERE "Status" != ?', ["Done"])
render(rows)
}
addEventListener("think:data", load)
load()Events that arrive close together are gathered into one event per table.
Live state
think.state is a JSON object per page and per person, kept by the room and synced across that person's open tabs. Use it for filters, selections and other view state that should follow the person.
think.setState({ filter: "mine", selected: "62e757cc63f9" }) // shallow merge; null removes a key
render(think.state)
addEventListener("think:state", (e) => render(e.detail.state)) // e.detail.by is "web" or "mcp"setStatedoes a shallow merge and does not firethink:statein the page that called it.- Right after the page opens,
think.statecan be empty for a moment before the saved state arrives inthink:state. Draw from both, and callsetStateonly when the person acts, so a default does not overwrite saved state. - Up to 16,000 characters per page and person.
- State is not stored in the database and is not part of page versions.
- State belongs to one person. Data shared between people belongs in a table.
- Guests who are not signed in, and fixed shared versions, keep state only inside the open tab.
Reading state from an agent
Over MCP, get_page_state and set_page_state read and write the caller's own state for a page. With since and wait, a read waits up to 25 seconds for the state to change.