Web Streams API — standard JS API for chunked data processing. ReadableStream / WritableStream / TransformStream. Native in browsers + Node.js 18+. Enables fetch streaming response, on-the-fly compression, progressive rendering, SSE (Server-Sent Events). Key enabler for AI streaming responses, large file uploads.
Below: details, example, related terms, FAQ.
Free online tool — HTTP header checker: instant results, no signup.
// Stream fetch response
const response = await fetch('/api/llm-chat');
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
output.textContent += decoder.decode(value);
}
// TransformStream — decrypt on-the-fly
const decryptor = new TransformStream({
transform(chunk, ctl) { ctl.enqueue(decrypt(chunk)); }
});The ReadableStream interface of the Web Streams API provides a means to read data in a stream format. This allows developers to handle data that is received in chunks, rather than waiting for the entire dataset to be available. A ReadableStream can be constructed using a ReadableStreamConstructor which takes an optional underlyingSource object that defines the data source.
Here’s a breakdown of key components:
ReadableStreamDefaultController allows you to control the stream. It provides methods like enqueue(), close(), and error() to manage the stream's data flow.ReadableStream by providing methods to read the data chunks asynchronously.To create a simple ReadableStream, you can use the following code:
const stream = new ReadableStream({
start(controller) {
// Initialize your data source
},
pull(controller) {
// Pull data from the source
},
cancel() {
// Cleanup if needed
}
});This structure allows for efficient data handling, especially useful in applications where data is generated or fetched incrementally.
Utilizing the ReadableStream can greatly enhance data handling in web applications. Below are practical examples demonstrating how to implement and use ReadableStream in various scenarios.
When fetching data from an API, you can utilize streams to process data as it arrives:
fetch('https://api.example.com/data')
.then(response => {
const reader = response.body.getReader();
return reader.read();
})
.then(({ done, value }) => {
// Process the chunk of data
});For large file uploads, you can read the file in chunks using ReadableStream:
const fileStream = new ReadableStream({
start(controller) {
const reader = fileInput.files[0].stream().getReader();
function push() {
reader.read().then(({ done, value }) => {
if (done) {
controller.close();
return;
}
controller.enqueue(value);
push();
});
}
push();
}
});Using ReadableStream with SSE can enhance real-time data updates:
const eventSource = new EventSource('https://api.example.com/sse');
const stream = new ReadableStream({
start(controller) {
eventSource.onmessage = (event) => {
controller.enqueue(event.data);
};
},
cancel() {
eventSource.close();
}
});These examples illustrate the versatility of ReadableStream for efficient data handling in modern web applications.
The ReadableStream interface from the Web Streams API provides significant performance benefits, especially when dealing with large datasets or real-time data processing. Here’s how it enhances performance:
With traditional data fetching methods, the entire dataset must be downloaded before any processing can begin. However, ReadableStream allows for incremental processing of data as it arrives. This is particularly useful in scenarios such as:
Using streams reduces the memory footprint of applications. Instead of loading large datasets entirely into memory, you can read and process smaller chunks. This is especially beneficial for:
ReadableStream allows for better network utilization by enabling features such as:
In summary, adopting the ReadableStream interface not only enhances application performance but also provides a more responsive and efficient user experience.
Promise: one result. Stream: a sequence of chunks. For large responses (LLM, file upload) streams are critical — they do not block memory.
100% modern browsers (Chrome 89+, Firefox 102+, Safari 14.5+). Node.js 18+ has full Web Streams API.
compressionStream API (2024) — native gzip/deflate via Web Streams. Replaces pako, zlib usage.
Free plan — 10 monitors, checks every 5 min, no card required. Upgrade for 1-minute interval and multi-region monitoring.