Skip to content

Rivet.DurableStream reference

Source: packages/rivet/src/DurableStream.ts

A Durable Stream: an append-only log with a durable, replayable history, one per key, served by Rivet Services. Each stream is its own Rivet actor. Declare it once with its message Schema; yield* it in any runtime for a typed client that appends, reads, and follows each stream live.

The messages’ wire format is a Layer, StreamFormat.Json or another, so a program only bundles the format it uses. It’s fixed when a stream is created.

import * as Rivet from "@alchemy.run/rivet";
import * as Schema from "effect/Schema";
export const Message = Schema.Struct({ user: Schema.String, text: Schema.String });
export class ChatLog extends Rivet.DurableStream<ChatLog>()("ChatLog", {
schema: Message,
ttl: "7 days",
}) {}
const log = yield* ChatLog;
yield* log.append("lobby", { user: "sam", text: "hello" });
const { messages, next } = yield* log.read("lobby");
const live = log.tail("lobby", { from: next });
import * as StreamFormat from "@alchemy.run/rivet/StreamFormat";
Effect.provide(Layer.mergeAll(Default, Services, StreamFormat.Json));