Emit events
Task: announce that something changed so clients can react.
Declare the events
Section titled “Declare the events”An #[app::event] enum in the #[app::logic] module. The variant name is the
event name; the fields are its data (camelCase keys; u64, i64 and bytes as
strings). From apps/kv-store:
#[app::event]pub enum Event { /// A new key was stored. Inserted { key: String, value: String }, /// An existing key's value was replaced. Updated { key: String, value: String }, /// A key and its value were removed. Removed { key: String }, /// Every key was removed from a non-empty store. Cleared,}The events and their fields are listed in the app’s ABI.
Emit them
Section titled “Emit them”pub fn update_if_exists(&mut self, key: String, value: String) -> app::Result<bool> { if !self.entries.contains(&key)? { return Ok(false); } self.entries.insert(&key, &value)?; env::emit(Event::Updated { key, value }, Audience::All)?; Ok(true)}env::emit(event, audience) adds the event to the run’s outcome. It reaches
clients only if the call succeeds and its entry commits; a failed call emits
nothing. env::emit_raw(name, &data, audience) emits any serde value by name.
Choose the audience
Section titled “Choose the audience”Audience |
Receives it |
|---|---|
All |
Everyone subscribed to the app. |
Caller |
The caller of the run. |
Accounts(vec![..]) |
These accounts. |
Team(id) |
The members of a team. |
Org |
The members of the app’s org. |
OrgAdmins |
The admins of the app’s org. |
The node resolves the audience when it delivers, against the org at that moment.
Read them
Section titled “Read them”Clients follow GET /apps/{app}/events?from=N: server-sent events, the
committed events from log index N, then live ones
(Node API). mero-js and calimero-client-py wrap the
stream; meroctl context watch prints it.