[]previous and next page

Live data

Three ways to push data after the page loads, and the smallest one that fits.

For agentsUpdated

Dusk has three ways for the server to keep talking to the page. Take the smallest one that does the job.

Use a timeline model to compare waiting for data with streaming the page shell first.

Explore streaming

You need Use
A stream of results from one call A server function with stream: true
A feed the server pushes A server-sent events route
Two-way traffic, like chat A WebSocket route

An SSE route exports SSE. The browser reconnects on its own and sends the last id it saw.

src/routes/live/prices.tsts
import type { RequestEvent, SseStream } from "@shadow-js/dusk/server";

export function SSE(event: RequestEvent, stream: SseStream): void {
  void event;
  stream.send({ event: "price", data: { value: 42 } }).catch(() => undefined);
}

A WebSocket route exports WS. upgrade runs before the socket opens, so it is the place to check who is asking.

src/routes/live/chat.tsts
import type { WebSocketHandlers } from "@shadow-js/dusk/server";

export const WS: WebSocketHandlers = {
  upgrade(event) {
    if (event.cookies.get("session") === undefined) return new Response(null, { status: 401 });
    return undefined;
  },
  message(ws, message) {
    if (typeof message === "string") ws.send(message);
  },
};

These are single-server. When you need named channels, resume after a reconnect, presence or more than one server, use @shadow-js/realtime, which builds on these two routes.

Next: deploy. Limits, heartbeats and shutdown are in Real time.