0Pricing
Sveltejs Academy · Lesson

Streaming with ReadableStream

Stream large responses progressively using the Web Streams API in +server.js.

Why Stream

Large responses can be sent progressively so the client starts receiving data immediately.

ReadableStream API

Use the standard Web Streams API to build a streaming response.

const stream = new ReadableStream({
  async start(controller) {
    for (const chunk of source) {
      controller.enqueue(new TextEncoder().encode(chunk));
    }
    controller.close();
  }
});
return new Response(stream);

All lessons in this course

  1. +server.js: GET POST PUT DELETE
  2. Request and RequestEvent
  3. Returning JSON Responses
  4. Streaming with ReadableStream
← Back to Sveltejs Academy