Skip to main content
Concepts

Reading

Load a stream's history from any offset and pick up where you left off.

Readers start at an offset and move forward. An offset is a position in the stream, and every read tells you the offset to continue from.

Load everything so far

import { stream } from "@durable-streams/client";

const url = "http://127.0.0.1:8642/durable-streams/v1/stream/chat/session-1";

// Everything written so far, then stop
const res = await stream<{ token: string }>({ url, offset: "-1", live: false });
const messages = await res.json();

console.log(messages);
console.log("read from here next:", res.offset);

res.offset is where the next read should start.

Pick up where you left off

Save the latest offset, for example in localStorage, and pass it back as offset next time. You get every message after it: nothing missed, nothing repeated.

  • -1 starts at the beginning.
  • now skips history and starts at the end.

Offsets are opaque strings. Store them and send them back as-is.

Know when a stream is done

A closed stream tells readers that nothing more will arrive, so they can stop waiting. Without closing, a reader cannot tell “nothing yet” from “nothing ever”.

Next steps