subscribe
Open a live feed of what is landing in a space, and resume it exactly by log position.
subscribe(query?: SubscribeQuery): Promise<Subscription>subscribe(request: SubscribeRequest): Promise<Subscription>Parameters
sessions64-bit integer[]optionalOnly these sessions.
kindsstring[]optionalOnly these memory kinds.
memoriesstring[]optionalOnly these memory ids.
includeHistorybooleanoptionalReplay what is already there before going live.
fromLsn64-bit integeroptionalResume at a log position. Pass the lsn from the last event you processed, or the one an encode returned, and you get an exact continuation rather than an approximate one.
maxInflightintegeroptionalServer-side backpressure bound.
Returns
event.eventTypestringalwaysWhat happened.
event.memoryId / sessionId / text / kind / salience—alwaysThe memory the event is about.
event.lsn64-bit integeralwaysThe log position. Persist this, not a count — it is the only value a resume accepts.
event.timestampUnixNanos64-bit integeralwaysWhen.
event.graph / edgeobjectGraph and edge detail, on events that carry it.
event.stageKind / stageOutcome / stage—Write-pipeline stage progress, on stage events.
closed.finalLsn64-bit integeralwaysWhere the feed stopped. Resume from here.
closed.reasonstringalwaysWhy it stopped.
Example
const feed = await brain.subscribe({ includeHistory: false, fromLsn: lastLsn });
try {
for await (const item of feed) {
if (item.kind === "closed") {
lastLsn = item.closed.finalLsn;
break;
}
lastLsn = item.event.lsn;
handle(item.event);
}
} finally {
await feed.close();
}The iterator finishes on a clean close. Code that treats the end of
iteration as a crash will reconnect in a tight loop; code that does not
reconnect at all will go quiet. Read the close reason, and resume from
finalLsn.
Subscribing is never retried for you
A retry would reopen the feed at the original position and re-deliver everything already seen. Where to resume is the caller's decision, because only the caller knows where it got to. See Retries.
The two tiers' shapes differ
HTTP (SSE)FeedItemYields a tagged union — either an event or a close record — so the end of the feed is a value you handle, not an exception. Also exposes the last SSE event id.
Wire (multiplexed)SubscriptionEventYields events directly. Offers nextEvent() for a pull-based read, an
unsubscribe() that tells the server to stop and waits for its
acknowledgement, and a close() that drops the local stream without
telling the server. Prefer unsubscribe() unless you are already tearing
the connection down.
The wire filter is richer: alongside sessions, kinds and memory ids it takes a similarity filter and a list of spaces, so one feed can span spaces.
Was this page helpful?