WritableStream
The WritableStream
interface of the Streams API provides a standard abstraction for writing streaming data to a destination, known as a sink.
This object comes with built-in backpressure and queuing.
Constructor
WritableStream()
-
Creates a new
WritableStream
object.
Properties
WritableStream.locked
Read only-
A boolean indicating whether the
WritableStream
is locked to a writer.
Methods
WritableStream.abort()
-
Aborts the stream, signaling that the producer can no longer successfully write to the stream and it is to be immediately moved to an error state, with any queued writes discarded.
WritableStream.close()
-
Closes the stream.
WritableStream.getWriter()
-
Returns a new instance of
WritableStreamDefaultWriter
and locks the stream to that instance. While the stream is locked, no other writer can be acquired until this one is released.
Examples
The following example illustrates several features of this interface. It shows the creation of the WritableStream
with a custom sink and an API-supplied queueing strategy. It then calls a function called sendMessage()
, passing the newly created stream and a string. Inside this function it calls the stream's getWriter()
method, which returns an instance of WritableStreamDefaultWriter
. A forEach()
call is used to write each chunk of the string to the stream. Finally, write()
and close()
return promises that are processed to deal with success or failure of chunks and streams.
const list = document.querySelector('ul');
function sendMessage(message, writableStream) {
// defaultWriter is of type WritableStreamDefaultWriter
const defaultWriter = writableStream.getWriter();
const encoder = new TextEncoder();
const encoded = encoder.encode(message, { stream: true });
encoded.forEach((chunk) => {
defaultWriter.ready
.then(() => {
return defaultWriter.write(chunk);
})
.then(() => {
console.log("Chunk written to sink.");
})
.catch((err) => {
console.log("Chunk error:", err);
});
});
// Call ready again to ensure that all chunks are written
// before closing the writer.
defaultWriter.ready
.then(() => {
defaultWriter.close();
})
.then(() => {
console.log("All chunks written");
})
.catch((err) => {
console.log("Stream error:", err);
});
}
const decoder = new TextDecoder("utf-8");
const queuingStrategy = new CountQueuingStrategy({ highWaterMark: 1 });
let result = "";
const writableStream = new WritableStream({
// Implement the sink
write(chunk) {
return new Promise((resolve, reject) => {
var buffer = new ArrayBuffer(1);
var view = new Uint8Array(buffer);
view[0] = chunk;
var decoded = decoder.decode(view, { stream: true });
var listItem = document.createElement('li');
listItem.textContent = "Chunk decoded: " + decoded;
list.appendChild(listItem);
result += decoded;
resolve();
});
},
close() {
var listItem = document.createElement('li');
listItem.textContent = "[MESSAGE RECEIVED] " + result;
list.appendChild(listItem);
},
abort(err) {
console.log("Sink error:", err);
}
}, queuingStrategy);
sendMessage("Hello, world.", writableStream);
You can find the full code in our Simple writer example.
Backpressure
Because of how backpressure is supported in the API, its implementation in code may be less than obvious. To see how backpressure is implemented look for three things:
- The
highWaterMark
property, which is set when creating the counting strategy (line 35), sets the maximum amount of data that theWritableStream
instance will handle in a singlewrite()
operation. In this example, it's the maximum amount of data that can be sent todefaultWriter.write()
(line 11). - The
defaultWriter.ready
property returns a promise that resolves when the sink (the first property of theWritableStream
constructor) is done writing data. The data source can either write more data (line 9) or callclose()
(line 24). Calling close() too early can prevent data from being written. This is why the example callsdefaultWriter.ready
twice (lines 9 and 22). - The
Promise
returned by the sink'swrite()
method (line 40) tells theWritableStream
and its writer when to resolvedefaultWriter.ready
.
Specifications
Specification |
---|
Streams Standard # ws-class |
Browser compatibility
BCD tables only load in the browser
See also
- WHATWG Stream Visualizer, for a basic visualization of readable, writable, and transform streams.