Keep a stream
Append entries to a log in a luna, read it a page at a time by offset, and choose how long entries stay.
Some data only grows, such as the messages of a chat room. A table can hold it, but you then trim old rows yourself, and each reader must track where it stopped.
A stream is an append-only log inside a luna. Each entry gets an offset that never changes, and the stream drops its oldest entries by the rules you set.
Declare a stream and append to it
The streams option of defineLuna names each stream. stream takes the type of an entry.
import { defineLuna, stream, t } from "luna";
export const Room = defineLuna({
name: "Room",
key: { id: t.string() },
streams: {
messages: stream(t.object({ author: t.string(), text: t.string() })),
},
handlers: (h) => ({
say: h({ args: { author: t.string(), text: t.string() } }, (ctx, { author, text }) => {
return ctx.streams.messages.append({ author, text });
}),
}),
});
ctx.streams.messages.append returns the entry’s offset. The first entry of a stream gets 0, the next 1, and so on. appendMany takes a list of entries, appends them in order and returns the first one’s offset.
Entries are stored with the handler’s other writes. If the handler fails, its entries were never there, and the next append reuses their offsets. Each entry is checked against the stream’s type and is at most 1 MiB as JSON.
How a stream differs from a table and an event
| Table | Stream | Event | |
|---|---|---|---|
| Holds | Rows you insert, change and delete | Entries you append and never change | Facts other luna types hear |
| Each item has | A primary key | An offset that never changes | A place in the emitting luna’s log |
| Read by | This luna’s handlers | This luna’s handlers, lunas that subscribe, apps | Luna types that listen |
| Kept | Until you delete it | By its retention, forever by default | By its retention, 7 days by default |
Read a page of entries
read returns up to limit entries from offset from on. from is 0 and limit is 100 unless you pass them, and limit is at most 1,000.
history: h({ args: { from: t.int() }, query: true }, (ctx, { from }) => {
return ctx.streams.messages.read({ from, limit: 50 });
}),
A page has these fields:
| Field | Holds |
|---|---|
entries |
Each entry’s offset, at (when it was appended, in milliseconds since the epoch) and data |
first |
The oldest offset the stream still keeps |
next |
The offset to pass as from for the next page |
end |
The offset the next append will get |
When next equals end, you have read everything. A from older than first starts at first, so a reader that fell behind skips what was dropped.
Choose how long entries stay
A stream keeps every entry unless you give it a retention.
messages: stream(t.object({ author: t.string(), text: t.string() }), {
retention: { maxAge: "30d", maxCount: 1_000 },
}),
| Option | Does |
|---|---|
maxAge |
Drops entries older than this, such as "30d", or a number of milliseconds |
maxCount |
Keeps at most this many entries |
maxBytes |
Keeps at most this many bytes of entries as JSON. The newest entry always stays |
The oldest entries go first. An entry past maxAge is no longer read, and it is deleted at the next append.
Make each append visible at once
With live: true, each append is stored on its own as soon as it is made. Readers see it while the handler still runs, and it stays even if the handler then fails.
typing: stream(t.string(), { live: true, retention: { maxCount: 100 } }),
Use a live stream for progress or output that a long handler sends as it goes. A query handler cannot append to a live stream. A deploy that turns a stream live, or back, is refused. Declare a new stream under another name instead.
Remove a stream
A deploy that drops a stream from the luna type deletes the stream in each luna, as that luna moves to the new version. luna deploy lists the stream among the data it deletes, and asks you to confirm before it goes ahead.
Next steps
- Roll up a stream keeps counts and sums per hour or day.
- Subscribe to a stream runs a handler in another luna for each entry.
- Follow a stream from an app reads a stream live in a browser or a phone.