Keep a view live
Task: show a list, a count or a record that stays current on screen, without reading the view again on a timer or on every event.
A client watches a view. The node runs it once, records what it read, and runs it again only when an entry changes something it read. When the answer differs, the node pushes it to the client. How the node does it, every frame and every limit are in Live views.
Mark the view
Section titled “Mark the view”Add subscribable to #[app::view]. From apps/fixture-kv:
/// The value of `key`, or `null`. Watches of it run at most every 250 ms.#[app::view(subscribable(interval = "250ms"))]pub fn get(&self, key: String) -> app::Result<Option<String>> { Ok(self.entries.get(&key)?.map(|e| e.value))}
/// Every entry (up to 1000).#[app::view(subscribable)]pub fn entries(&self) -> app::Result<std::collections::BTreeMap<String, String>> { let page = self.entries.scan(FindOpts::default().limit(LIST_LIMIT))?; Ok(page.items.into_iter().map(|e| (e.key, e.value)).collect())}The ABI then says "subscribable": true for both, and "watchIntervalMs": 250
for get. A view without subscribable cannot be watched: the node answers
400 NOT_SUBSCRIBABLE. Only views take it; a call, timer or action cannot.
Choose the interval
Section titled “Choose the interval”The interval is the most often one watch of the view gets a new answer. The first change after a quiet spell is sent at once; later changes within the interval wait and go out as one answer.
subscribablealone: 1 second.subscribable(interval = "500ms")orinterval = "2s": anything from 250 ms to 10 s, inmsors. Anything else does not compile.
Pick the slowest rate your screen can live with. Each run costs the node a view
run, and every watcher of a busy view shares the cost. A client may ask for a
longer interval (intervalMs), never a shorter one.
Keep it watchable
Section titled “Keep it watchable”The node keeps what the view read: each key it got, and each range it scanned, cut after the last key the scan returned. Write views that read little:
- Read a page, not the table. A view that scans 50 records and stops is re-run only by writes among those 50 (or before the 51st). A view that walks the whole table is re-run by every write to it.
- Stay under the caps. A view that reads more than 4,096 keys, scans more
than 64 separate ranges, or more than 256 KiB of keys cannot be watched:
adding the watch answers
422 READ_SET_UNBOUNDED. A watch whose view later grows past them ends withread-set-unbounded. - Do not call other apps. A view that calls another app’s view
(
env::xcall_view, or a typed client of ausesapp) cannot be watched, since the node cannot see what that app read. - Search reads the whole table. A view that calls
Table::searchis re-run by any write to that table’s records, since any record may now match. Watch a search only on a table that changes rarely. - The clock re-runs on every entry. A view that reads
env::time_now()or another clock is re-run by every entry the app applies. - Org reads follow the org. A view that reads
env::org_role,env::in_team,env::grant_scopeor the app’s own record is re-run by a system change that may change that fact, such as a promotion or a removal. - Who called it. A view that never reads the caller shares one run among
every watcher of the same arguments. One that reads
env::caller()runs once per caller.
Watch it from a client
Section titled “Watch it from a client”In React, useView with live (from @calimero-network/mero-react):
const { data, ended } = useView<Record<string, string>>('entries', {}, { live: true });const connection = useConnectionState(); // 'idle' | 'connecting' | 'open' | 'reconnecting'data updates on its own. ended names why the node ended the watch
(access-lost, view-refused, read-set-unbounded); no update follows it.
With mero-js, client.watchView (Apps):
const watch = client.watchView<string | null>(app, 'get', { key: 'a' }, { onAnswer: (value, index) => render(value), onEnded: (reason) => console.warn('watch ended', reason),});watch.close();In Rust, calimero_client::Client::watch(app, view, args) gives the first
answer and a stream of WatchNotices, and keeps the watch across reconnects.
From the command line:
meroctl call -i my-kv entries --view --watch # one JSON line per answermeroctl call -i my-kv get --args '{"key":"a"}' --view --watch --limit 3Every watch of an app on one client shares one connection. When it drops, the client resumes it, and the node sends what changed meanwhile. After 30 seconds away the node forgets the watches, and the client adds them again on its own.
Test it
Section titled “Test it”TestApp runs views but not watches: test what the view answers, as for any
view. The node’s own tests of watches are in crates/node/tests/it/watches.rs.