bare-broadcast-channel
Reference for bare-broadcast-channel: multi-producer, multi-consumer inter-thread broadcast messaging for Bare.
bare-broadcast-channel provides multi-producer, multi-consumer broadcast messaging between Bare threads. A channel exposes a transferable handle, so other threads can join the same channel and exchange structured-cloned values over per-thread ports. It's a native addon.
npm i bare-broadcast-channelUsage
const BroadcastChannel = require('bare-broadcast-channel')
const { Thread } = Bare
const channel = new BroadcastChannel()
const consumer = (label) =>
new Thread(__filename, { data: { handle: channel.handle, label } }, async ({ handle, label }) => {
const BroadcastChannel = require('bare-broadcast-channel')
const port = BroadcastChannel.from(handle).connect()
console.log(label, 'got', await port.read())
await port.close()
})
consumer('a')
consumer('b')API
BroadcastChannel
const channel = new BroadcastChannel([options])
Create a channel. BroadcastChannel.MAX_PORTS is the maximum number of connected ports.
const channel = BroadcastChannel.from(handle[, options])
Reconstruct a channel from a transferred channel.handle (pass handle to a thread via its data).
channel.handle · channel.interfaces
The transferable handle, and the channel's interfaces.
const port = channel.connect()
Connect to the channel, returning a Port for this thread.
Port
await port.write(value[, options]) · port.writeSync(value[, options])
Broadcast a structured-cloneable value to peers; returns whether the write flushed.
const value = await port.read() · port.readSync()
Read the next broadcast value.
port.createReadStream([options]) · port.createWriteStream([options]) · port.createStream([options])
Stream interfaces over the port.
port.peers · port.ref() · port.unref() · await port.close()
Peer count, event-loop references, and teardown. Ports emit peers and close.
Related modules
Builds on bare-events, bare-stream, and bare-structured-clone (see Bare modules).
See also
bare-channel—point-to-point inter-thread messaging.- Bare runtime API—
Bare.Thread, the threads this broadcasts between. - Bare modules—the full
bare-*catalog.