API referenceWebSocket subscriptions

WebSocket subscriptions

Alpha, on the 3.6.x line. The message formats can still change within 3.x.

Endpoint

ws://your-host:3939/memory/ws?token=mns_live_xxx

A WebSocket upgrade cannot carry an Authorization header from a browser, so the API key goes in the token query parameter. A missing or invalid key closes the socket with code 1008. If the server cannot check the key’s user status it closes with 1011. When the server sets MNEMO_REGION and the workspace is locked to another region, the socket closes with 1008; if the region cannot be read, with 1011.

You only receive events from the workspace your key belongs to.

Client messages

Send JSON text frames. Malformed frames are ignored.

interface WsSubscription {
  subscribe?:   { channel: string; filter?: MnemoEventFilter };
  unsubscribe?: { channel: string };
  resubscribe?: { channels: string[]; lastEventId: string };
}
 
interface MnemoEventFilter {
  entityId?:   string; // compared with payload.id
  agentId?:    string; // compared with payload.agentId
  memoryType?: string; // compared with payload.memoryType
}

Example:

{ "subscribe": { "channel": "tasks.*" } }

Channels

A channel pattern is an exact channel name, a prefix ending in .*, or * for everything.

PatternEvents
facts.*fact.created, fact.updated, fact.archived
fact.createdNew facts only
tasks.*task.created, task.transitioned, task.completed
events.*event.recorded
session.*Every session event
session.{sessionId}session.collapsed when the server auto-collapses that session
agent.{workspaceId}.recallrecall.hit
reminder.{workspaceId}reminder.triggered
healthhealth.alert
federation.syncfederation.conflict

facts.*, tasks.* and events.* are aliases: the server publishes on the singular names (fact.created, task.created, event.recorded). To subscribe to one event type, use the singular name.

Server events

interface MnemoBusEvent {
  id?:       string;   // monotonic id, used for replay
  channel:   string;
  type:
    | "fact.created" | "fact.updated" | "fact.archived"
    | "task.created" | "task.transitioned" | "task.completed"
    | "event.recorded" | "recall.hit" | "session.collapsed"
    | "health.alert" | "federation.conflict" | "reminder.triggered";
  payload:   unknown;
  timestamp: string;   // ISO-8601 UTC
  traceId:   string;
}

A frame holds one event object, or a JSON array of events when several were buffered and sent together. Handle both.

Reconnecting

After a reconnect, send resubscribe with the id of the last event you processed. The server subscribes you to the channels again and replays what you missed. Filters are not restored by resubscribe.

If the id is too old, you get a gap frame instead and should catch up over REST with GET /memory/events:

interface WsGapFrame {
  type: "ws.gap";
  channel: string;
  oldestAvailable: string | null;
  message?: string;
}

Replay needs Redis. The buffer keeps the last 200 events of the workspace, whatever their channel, for one hour. The server finds lastEventId in it wherever that event was published, then replays the later events whose channel matches any pattern in channels: exact names, prefixes such as tasks.*, aliases or *. Each event is replayed once, in the order it was emitted, through the same workspace check as live events. When lastEventId is no longer in the buffer, you get one ws.gap frame per channel.

The cap covers the whole workspace, so a busy workspace reaches the gap sooner than a quiet channel would.

Backpressure

  • Each connection has a ring buffer of 100 events. When it is full, the oldest events are dropped.
  • A connection whose buffer stays full for five minutes is closed with code 1013. The check runs every 30 seconds.

See also