subscribe
Open a live feed of what is landing in a space, and resume it exactly by log position.
async fn subscribe(&self, query: &SubscribeQuery) -> Result<Subscription, BrainHttpError>async fn subscribe(&self, request: &SubscribeRequest) -> Result<Subscription<TcpStream>>Parameters
sessions64-bit integer[]optionalOnly these sessions.
kindsstring[]optionalOnly these memory kinds.
memoriesstring[]optionalOnly these memory ids.
include_historybooleanoptionalReplay what is already there before going live.
from_lsn64-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.
max_inflightintegeroptionalServer-side backpressure bound.
Returns
event.event_typestringalwaysWhat happened.
event.memory_id / session_id / 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.timestamp_unix_nanos64-bit integeralwaysWhen.
event.graph / edgeobjectGraph 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 integeralwaysWhere the feed stopped. Resume from here.
closed.reasonstringalwaysWhy 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); }
}
}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?