Brain Docs

Reference

TypeScript SDKInstallationHTTP clientWire client
subscribecancelStream
ErrorsRetriesPagination
Brain/Docs
✦ ASKBrain on GitHubDashboard
TypeScript SDKStreamssubscribe

subscribe

Open a live feed of what is landing in a space, and resume it exactly by log position.

PURPOSE

A live feed of what is landing in a space. The HTTP tier serves it over Server-Sent Events; the wire tier runs it as one stream on the multiplexed connection, so it does not occupy a socket of its own.

GET/v1/subscribeBearer API keystable
subscribe(query?: SubscribeQuery): Promise<Subscription>
Handshake credentialstable
subscribe(request: SubscribeRequest): Promise<Subscription>

Parameters

ParameterTypeRequired
sessions64-bit integer[]optional

Only these sessions.

kindsstring[]optional

Only these memory kinds.

memoriesstring[]optional

Only these memory ids.

includeHistorybooleanoptional

Replay what is already there before going live.

fromLsn64-bit integeroptional

Resume 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.

maxInflightintegeroptional

Server-side backpressure bound.

Returns

FieldTypePresence
event.eventTypestringalways

What happened.

event.memoryId / sessionId / text / kind / salience—always

The memory the event is about.

event.lsn64-bit integeralways

The log position. Persist this, not a count — it is the only value a resume accepts.

event.timestampUnixNanos64-bit integeralways

When.

event.graph / edgeobject

Graph and edge detail, on events that carry it.

event.stageKind / stageOutcome / stage—

Write-pipeline stage progress, on stage events.

closed.finalLsn64-bit integeralways

Where the feed stopped. Resume from here.

closed.reasonstringalways

Why 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();
}
A feed that ends is not an error

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

OptionTypeDefault / Env
HTTP (SSE)FeedItem

Yields 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)SubscriptionEvent

Yields 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.

See also
cancelStreamencode — where lsn comes from

Was this page helpful?

queryTrace

Run a query and get back what actually happened, with the measured latency.

cancelStream

Stop an in-flight multiplexed stream server-side — abandoning the call does not.

On this page

ParametersReturnsExampleThe two tiers' shapes differ
Suggest an editOpen in Playground