UNAVI Docs unavi.xyz
Build · Scripting API

wired:event

Local pub/sub messaging between scripts on the same peer.

Messages never cross the network; use shared scene properties for state that must synchronize across peers. Channels share a flat namespace, so prefix names as <namespace>:<package>/<name> and filter by senders when authenticity matters.

Interface messaging

Uses document-id, prim-ref, error.

spatial

record

Sphere centered on a prim in a writable document.

origin: prim-ref
radius: f32Radius in meters.

scope

variant

Delivery scope. Spatial messages reach overlapping spatial listeners and all global listeners; global messages reach global listeners only.

global
spatial(spatial)

message

record
channel: string
payload: list<u8>
sender: option<document-id>Sender document ID, or none for host-originated messages.
distance: option<f32>Distance in meters between origins when both sender and listener are spatial.
sent-at: f64Host clock timestamp in seconds when emitted.
claim-token: option<u64>Token set when audience was specified; pass to message-subscription.claim to claim exclusive handling.

message-subscription

resource

Queues up to 64 messages.

drain

methoddrain: func(max: u32) -> list<message>

dropped

methoddropped: func() -> u64

claim

methodclaim: func(token: u64) -> bool

Claims an audience-addressed message; returns true for the single winning listener. Remembers the 256 most recent tokens.

emit

funcemit: func(channel: string, payload: list<u8>, audience: option<list<document-id>>, scope: scope) -> result<_, error>

Emits payload on channel to matching listeners (optionally restricted to audience). Limits: 256-byte channel, 64 KiB payload, 64 audience documents.

listen

funclisten: func(channels: list<string>, senders: option<list<document-id>>, scope: scope) -> result<message-subscription, error>

Subscribes to channels (optionally filtered to senders). Limits: 32 channels, 64 senders.

Source

/// Local pub/sub messaging between scripts on the same peer.
///
/// Messages never cross the network; use `shared` scene properties for state
/// that must synchronize across peers. Channels share a flat namespace, so
/// prefix names as `<namespace>:<package>/<name>` and filter by `senders` when
/// authenticity matters.
package wired:event;

interface messaging {
  use wired:core/ids.{document-id, prim-ref};
  use wired:core/error.{error};

  /// Sphere centered on a prim in a writable document.
  record spatial {
    origin: prim-ref,
    /// Radius in meters.
    radius: f32,
  }

  /// Delivery scope. Spatial messages reach overlapping spatial listeners and
  /// all global listeners; global messages reach global listeners only.
  variant scope {
    global,
    spatial(spatial),
  }

  record message {
    channel:     string,
    payload:     list<u8>,
    /// Sender document ID, or `none` for host-originated messages.
    sender:      option<document-id>,
    /// Distance in meters between origins when both sender and listener are
    /// spatial.
    distance:    option<f32>,
    /// Host clock timestamp in seconds when emitted.
    sent-at:     f64,
    /// Token set when `audience` was specified; pass to
    /// `message-subscription.claim` to claim exclusive handling.
    claim-token: option<u64>,
  }

  /// Emits `payload` on `channel` to matching listeners (optionally restricted
  /// to `audience`). Limits: 256-byte channel, 64 KiB payload, 64 audience
  /// documents.
  emit: func(
    channel:  string,
    payload:  list<u8>,
    audience: option<list<document-id>>,
    scope:    scope,
  ) -> result<_, error>;

  /// Subscribes to `channels` (optionally filtered to `senders`). Limits: 32
  /// channels, 64 senders.
  listen: func(
    channels: list<string>,
    senders:  option<list<document-id>>,
    scope:    scope,
  ) -> result<message-subscription, error>;

  /// Queues up to 64 messages.
  resource message-subscription {
    drain:   func(max: u32) -> list<message>;
    dropped: func() -> u64;
    /// Claims an audience-addressed message; returns `true` for the single
    /// winning listener. Remembers the 256 most recent tokens.
    claim:   func(token: u64) -> bool;
  }
}