folkwaysDocs
Primitives

Stream

Ordered, replayable events for messages, orders and AI output, with cursors, retention, gaps and idempotent retries.

A stream event is kept in order with every other stream event in its topic, for a bounded time, so a subscriber can replay what it missed. It is the default type, and the right one whenever a user would be upset to miss an event.

Use it for: chat messages, comments, orders and status changes, notifications, AI output that must survive a page reload.

How it behaves

  • Ordered. Each stream event gets the next position in its topic, (epoch, seq), and every subscriber sees the same order.
  • Replayable. A subscriber picks where to start: only new events, the oldest still retained, the last N, or after a cursor it saved. It gets the replay, then live events, with nothing missed or repeated at the seam.
  • Bounded. A topic keeps stream events up to its rule's byte cap (5 MiB by default) and at most 24 hours; the oldest go first.
  • Retried safely. Every stream publish carries an idempotency key. Retrying with the same key within 10 minutes doesn't deliver it twice.
  • Permanent IDs. Each event gets a uid that never changes. Point replies, edits and receipts at it.

Positions and gaps

Save the cursor of the last event you processed. After a reconnect or a reload, subscribe after it to catch up. If the events after your cursor are gone, you get a gap first, with the oldest position still available.

Epoch 7 holds events 7:1 to 7:12. Events 7:1 to 7:3 are purged; 7:4 to 7:12 are retained, up to the rule's byte cap, 5 MiB by default, for at most 24 hours. A client that saved cursor 7:6 subscribes after it and gets 7:7 to 7:12 replayed, then live events. A client that saved 7:2 gets Gap with reason Expired and oldest 7:4, and with on gap from oldest the replay starts at 7:4. If the epoch changes from 7 to 8, a cursor from epoch 7 gets Gap with reason EpochLost.

Delivery and ordering covers cursors, gaps and idempotency in full.

Not a database

Stream history is short and may not survive a restart of the service. Keep the record of truth, the full chat history or the order log, in your own database, and use the stream to deliver it live and fill short gaps. A common pattern: your backend writes to its database, then publishes the event.

Rule settings

SettingDefaultMeaning
Retention (bytes)5 MiBHow much history each topic keeps
Payload limitSegment's limit, at most 60 KiBLargest event

In code

TypeScript
const room = client.topic('rooms/42');
room.subscribe((msg) => render(msg), { from: { last: 50 } });
const receipt = await room.publish('message', { text: 'Hi!' });

See Publishing and Subscribing.

On this page