Brain Docs

Reference

Rust SDKInstallationHTTP clientWire client
subscribecancel_stream
ErrorsRetriesPagination
Brain/Docs
✦ ASKBrain on GitHubDashboard
Rust 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
async fn subscribe(&self, query: &SubscribeQuery) -> Result<Subscription, BrainHttpError>
Handshake credentialstable
async fn subscribe(&self, request: &SubscribeRequest) -> Result<Subscription<TcpStream>>

Parameters

ParameterTypeRequired
sessions64-bit integer[]optional

Only these sessions.

kindsstring[]optional

Only these memory kinds.

memoriesstring[]optional

Only these memory ids.

include_historybooleanoptional

Replay what is already there before going live.

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

max_inflightintegeroptional

Server-side backpressure bound.

Returns

FieldTypePresence
event.event_typestringalways

What happened.

event.memory_id / session_id / 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.timestamp_unix_nanos64-bit integeralways

When.

event.graph / edgeobject

Graph and edge detail, on events that carry it.

event.stage_kind / stage_outcome / stage—

Write-pipeline stage progress, on stage events.

closed.final_lsn64-bit integeralways

Where the feed stopped. Resume from here.

closed.reasonstringalways

Why it stopped.

Example

let mut feed = brain
    .subscribe(&SubscribeQuery { include_history: Some(false), from_lsn: Some(last_lsn), ..Default::default() })
    .await?;

while let Some(item) = feed.next().await {
    match item? {
        FeedItem::Closed(c) => { last_lsn = c.final_lsn; break }
        FeedItem::Event(e) => { last_lsn = e.lsn; handle(e); }
    }
}
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
cancel_streamencode — where lsn comes from

Was this page helpful?

query_trace

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

cancel_stream

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