All of the guide

Live updates

When something changes, every page showing it can update by itself. The server broadcasts Turbo Stream HTML to a topic, and each page following that topic applies it.

browser A posts a message
  → the route saves it and calls broadcast("messages", <Stream action="prepend" …>)
  → void/live sends it to every connection subscribed to "messages" (a Durable Object fans it out)
  → in browsers A, B, C…, <turbo-live-stream-source topic="messages"> hands it to Turbo
  → Turbo puts the message first

The three pieces

The stream. src/live.ts defines one void/live stream for the app, and routes/live.ts gives it an endpoint:

export const live = defineLiveStream({
  id: "app",
  allowAnonymousControl: true,
  onSubscribe(ctx) {
    if (ctx.topic === "messages") return;
    return new Response("Forbidden", { status: 403 });
  },
});

onSubscribe decides who may read each topic. Every topic is refused until you allow it, because topic names are easy to guess. A private topic checks the signed-in user (see Void's live guide on authorization).

The source element. src/live-stream-source.ts defines <turbo-live-stream-source topic="…">. Put it in a page to follow a topic:

<turbo-live-stream-source topic="messages"></turbo-live-stream-source>

void/live delivers JSON events. The element subscribes to the topic, and passes each event's HTML to Turbo, which it registered as a stream source with connectStreamSource. turbo-rails' <turbo-cable-stream-source> does the same over Action Cable. One connection serves every element on the page, and the subscription survives Turbo navigation. Once broadcasts reach it, the element has a connected attribute (as turbo-rails' element does), which tests wait for.

The broadcast. After the write, render the change once and publish it (src/turbo.tsx's broadcast):

await broadcast("messages", (
  <Stream action="prepend" target="messages">
    <MessageItem message={message} />
  </Stream>
));

The page that posted the message gets the same prepend twice, once in its form answer and once from the broadcast. Turbo's prepend (like append) first removes a child with the same id, so the message shows once.

The simplest broadcast: refresh

Instead of describing the change, tell every page to reload itself:

await broadcast("messages", <Stream action="refresh" />);

Each page fetches itself again and morphs into the new version: only what changed is touched, the scroll position stays, and the field someone is typing in keeps its text (the layout's turbo-refresh-* meta tags turn this on). Other fields go back to what the server rendered. Lazy frames load again, so their content is current too. It needs no ids and no templates. The cost is one request per open page.

Check

flows/live-updates.yaml checks this, in a browser (vp run test:browser) and in both apps (Testing). By hand: open the app in two browser windows that don't share cookies (one of them private), post a message in one, and it appears in the other, once.

When it does not work

Symptom Cause
Nothing arrives The topic is refused in onSubscribe (the /live request returns 403), or the page has no <turbo-live-stream-source>
It arrives twice The broadcast element has no id
It stops after navigating The element was removed with no replacement on the new page; put it in every page that follows the topic