Live views & events
A view answers once. A live view keeps answering: the node remembers what the view read, runs it again only when one of those things changes, and pushes the new answer to every client watching it. A screen built on live views never polls and never re-reads a view after each event. Live views need nodes on merod 1.0.0-rc.40 or later.
Mark a view live
Section titled “Mark a view live”Add subscribable to a view’s attribute:
/// How many keys the store holds. #[app::view(subscribable)] pub fn len(&self) -> app::Result<u64> { Ok(self.items.len()?) }
/// The value under `key`, at most twice a second. #[app::view(subscribable(interval = "500ms"))] pub fn get(&self, key: String) -> app::Result<Option<String>> { Ok(self.items.get(&key)?) }intervalis the most often one watch gets a new answer:"<n>ms"or"<n>s", from 250 ms to 10 s. Without it, 1 s. A value outside that range does not compile.- The attribute reaches the app’s ABI (
"subscribable": true,"watchIntervalMs": 500), so it ships with the app’s next release. Watching a view without it is refused with400 NOT_SUBSCRIBABLE. - Only views can be live. The view itself does not change: it is still called as an ordinary view too.
How updates arrive
Section titled “How updates arrive”- At once, then when it changes. A watch starts with the view’s current answer. When a write changes something the view read, the node runs the view again, as the person watching, and sends the new answer.
- At most once per interval. The first change is sent at once; changes after it within the interval are merged into one update with the latest answer. A client may ask for a longer interval than the app’s, never a shorter one.
- Only new answers. When the view runs again and answers the same as last time, nothing is sent.
- Large answers. An answer over 64 KiB is not pushed: the client gets a notice that it changed and reads the view again. mero-js does this for you.
- Access is checked on every update. When the person watching loses access to the instance (they leave its
organization, their session is revoked), the watch ends with
access-lost; when the view itself starts refusing them (a team check, say), withview-refused. Nothing more is sent. - Reconnecting. All of a client’s watches of one app travel on one connection. If it drops, the client reconnects; within 30 seconds it gets back the same watches, with the current answer of each that changed meanwhile. After that, it adds them again. mero-js and meroctl do both.
- Shared work. Many people watching the same view with the same arguments share one run per change. A view
that never asks who is calling is shared across every caller; one that does (
env::caller(), a role or team check) runs once per caller.
What a live view should read
Section titled “What a live view should read”The node tracks what the view read, so that set must stay small: at most 4,096 keys, 64 ranges (a range is one
iteration over a collection, however many entries it returns), 256 KiB of keys, and no views of other apps. A view that reads more
cannot be watched (422 READ_SET_UNBOUNDED); one that grows past the limit while watched ends with
read-set-unbounded. Return one page of a collection, as the KV Store’s entries does, rather than all of it.
A view that reads the clock runs again on every write to the instance, since any write moves its time. Keep the clock out of views you watch where you can.
From JavaScript
Section titled “From JavaScript”client.watchView(app, view, args, options) watches a view. onAnswer gets the first answer, then each new one:
const watch = client.watchView<string | null>(app, 'get', { key: 'a' }, { onAnswer: (value, index) => render(value), // at once, then on each change onEnded: (reason) => console.warn('ended', reason), // 'access-lost', 'view-refused', 'read-set-unbounded' onError: (error) => console.error(error), // an app error; the watch carries on intervalMs: 2000, // optional: slower than the app's interval});
watch.close(); // or pass `signal` and abort itclient.watches.state(app) says how the app’s connection stands (idle, connecting, open, reconnecting), and
client.watches.onState(listener) reports each change. The generated KvStoreClient has no watch methods; use
watchView on the CalimeroClient it wraps.
From React
Section titled “From React”In @calimero-network/mero-react, useView takes live: true:
import { useView, useConnectionState } from '@calimero-network/mero-react';
function Count() { const { data, ended } = useView<string>('len', {}, { live: true }); const connection = useConnectionState();
if (ended) return <p>No longer shared with you.</p>; return ( <p> {data ?? '…'} keys {connection === 'reconnecting' && <span>reconnecting…</span>} </p> );}data updates on its own; ended is set when the node ends the watch. intervalMs slows it, as in mero-js.
Without live, useView reads once, and you call refetch yourself.
From the command line
Section titled “From the command line”meroctl call --instance kv len --view --watch{"index":3,"result":"1"}{"index":4,"result":"2"}It prints the answer now, then one JSON line per new answer, and {"ended":true,"reason":"…"} when the node ends
the watch. --limit N stops after N updates.
Limits
Section titled “Limits”| Limit | Value |
|---|---|
| A view’s interval (the app sets it) | 250 ms to 10 s, default 1 s |
A client’s intervalMs |
Only longer than the app’s, up to 10 s |
| An answer pushed whole | 64 KiB (past it, a notice and a read) |
| What a watched view reads | 4,096 keys, 64 ranges and 256 KiB of keys; no other apps’ views |
| A watched view’s arguments | 4 KiB (413 ARGS_TOO_LARGE) |
| Watches on one connection (one app, one client) | 64 (429 TOO_MANY_WATCHES) |
| Reconnecting to the same watches | Within 30 s |
Events and audiences
Section titled “Events and audiences”Live views are for what a screen shows. Events are for things that happen: a notification, a toast, a line in
an activity feed. A call emits them with env::emit(event, audience), and they go out only if the call succeeds.
The audience says who receives an event:
| Audience | Who receives it |
|---|---|
Audience::All |
Everyone who reaches the instance |
Audience::Caller |
Only the account whose call emitted it |
Audience::Accounts(vec![..]) |
The accounts listed (at most 1,000) |
Audience::Team(team) |
The members of that team of the instance’s organization |
Audience::Org |
The organization’s members (Member and up) |
Audience::OrgAdmins |
The organization’s admins and owner |
env::emit(Event::Assigned { task: id.clone() }, Audience::Accounts(vec![assignee]))?;env::emit(Event::BudgetChanged { amount }, Audience::OrgAdmins)?;The audience is judged when the event goes out, so a member removed from a team stops receiving that team’s events at once. A call’s answer carries only the events its caller is in the audience of.
Clients follow events with client.events(app) in mero-js, useSubscription in mero-react, or
meroctl instance watch. The node keeps the last 10,000 events of an instance, so a client that reconnects passes
the last index it saw and misses nothing.
For the full mechanics, see the core reference’s Reads, views & events and mero-js’s apps guide.