logoDenoby example

Intermediate

clideploy

HTTP Server: Streaming

Edit

An example HTTP server that streams a response back to the client.

function handler(_req: Request): Response {
Set up a variable to store a timer ID, and the ReadableStream.
  let timer: number | undefined = undefined;
  const body = new ReadableStream({
When the stream is first created, start an interval that will emit a chunk every second containing the current time.
    start(controller) {
      timer = setInterval(() => {
        const message = `It is ${new Date().toISOString()}\n`;
        controller.enqueue(new TextEncoder().encode(message));
      }, 1000);
    },
If the stream is closed (the client disconnects), cancel the interval.
    cancel() {
      if (timer !== undefined) {
        clearInterval(timer);
      }
    },
  });
Return a response with the stream as the body.
  return new Response(body, {
    headers: {
      "content-type": "text/plain",
      "x-content-type-options": "nosniff",
    },
  });
}
To start the server on the default port, call `Deno.serve` with the handler.
Deno.serve(handler);

Run this example locally using the Deno CLI:

deno run --allow-net https://examples.deno.land/http-server-streaming.ts

Try this example in a Deno Deploy playground: