subscribe
Open a live feed of what is landing in a space, and resume it exactly by log position.
subscribe(sessions=None, kinds=None, memories=None, include_history=None, from_lsn=None, max_inflight=None) -> Subscriptionsubscribe(request: SubscribeRequest) -> SubscriptionParameters
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
with brain.subscribe(include_history=False, from_lsn=last_lsn) as feed:
for item in feed:
if item.kind == "closed":
last_lsn = item.closed.final_lsn
break
last_lsn = item.event.lsn
handle(item.event)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?