Multi-consumer
History
broadcast(options?): Object
Objectnumber65536.string'strict', 'unbounded', 'drop-oldest', or
'drop-newest'. Default: 'strict'.AbortSignalObjectWritableBroadcastChannelCreate a push-model multi-consumer broadcast channel. A single writer pushes data to multiple consumers. Each consumer has an independent cursor into a shared buffer.
import { broadcast, text } from 'node:stream/iter'; const { writer, broadcast: bc } = broadcast(); // Create consumers before writing const c1 = bc.push(); // Consumer 1 const c2 = bc.push(); // Consumer 2 // Producer and consumers must run concurrently. Awaited writes // block when the buffer fills until consumers read. const producing = (async () => { await writer.write('hello'); await writer.end(); })(); const [r1, r2] = await Promise.all([text(c1), text(c2)]); console.log(r1); // 'hello' console.log(r2); // 'hello' await producing;
const { broadcast, text } = require('node:stream/iter'); async function run() { const { writer, broadcast: bc } = broadcast(); // Create consumers before writing const c1 = bc.push(); // Consumer 1 const c2 = bc.push(); // Consumer 2 // Producer and consumers must run concurrently. Awaited writes // block when the buffer fills until consumers read. const producing = (async () => { await writer.write('hello'); await writer.end(); })(); const [r1, r2] = await Promise.all([text(c1), text(c2)]); console.log(r1); // 'hello' console.log(r2); // 'hello' await producing; } run().catch(console.error);
broadcast.cancel(reason?): void
ErrorCancel the broadcast. All consumers receive an error.
The number of active consumers.
broadcast.push(...transforms?, options?): AsyncIterable
ObjectAbortSignalAsyncIterableUint8Array[]Create a new consumer. Each consumer receives all data written to the broadcast from the point of subscription onward. Optional transforms are applied to this consumer's view of the data.
broadcast[Symbol.dispose](): void
Alias for broadcast.cancel().
Broadcast.from(input, options?): Object
AsyncIterable | Iterable | BroadcastChannelObjectbroadcast().Object{ writer, broadcast }Create a BroadcastChannel from an existing source. The source is consumed
automatically and pushed to all subscribers.
share(source, options?): Share
Create a pull-model multi-consumer shared stream. Unlike broadcast(), the
source is only read when a consumer pulls. Multiple consumers share a single
buffer.
import { from, share, text } from 'node:stream/iter'; const shared = share(from('hello')); const c1 = shared.pull(); const c2 = shared.pull(); // Consume concurrently to avoid deadlock with small buffers. const [r1, r2] = await Promise.all([text(c1), text(c2)]); console.log(r1); // 'hello' console.log(r2); // 'hello'
const { from, share, text } = require('node:stream/iter'); async function run() { const shared = share(from('hello')); const c1 = shared.pull(); const c2 = shared.pull(); // Consume concurrently to avoid deadlock with small buffers. const [r1, r2] = await Promise.all([text(c1), text(c2)]); console.log(r1); // 'hello' console.log(r2); // 'hello' } run().catch(console.error);
Share.from(input, options?): Share
Create a Share from an existing source.
share.cancel(reason?): void
ErrorCancel the share. All consumers receive an error.
The number of active consumers.
share.pull(...transforms?, options?): AsyncIterable
ObjectAbortSignalAsyncIterableUint8Array[]Create a new consumer of the shared source.
share[Symbol.dispose](): void
Alias for share.cancel().
shareSync(source, options?): SyncShare
Synchronous version of share().
SyncShare.fromSync(input, options?): SyncShare
The number of chunks currently buffered.
share.cancel(reason?): void
ErrorCancel the share. All consumers receive an error.
The number of active consumers.
share.pull(...transforms?, options?): Iterable
ObjectAbortSignalIterableUint8Array[]Create a new consumer of the shared source.
share[Symbol.dispose](): void
Alias for share.cancel().