LogoPear Docs
ReferencesBareModules

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-channel

Usage

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()
  })

const a = consumer('a')
const b = consumer('b')

const port = channel.connect()

while (port.peers < 2) await new Promise((r) => setTimeout(r, 10))

await port.write('hello')
await port.close()

a.join()
b.join()

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. Throws if the channel has reached BroadcastChannel.MAX_PORTS; note that this is a lifetime cap—port slots are not reused after a port is closed.

Port

await port.write(value) · port.writeSync(value)

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.

Builds on bare-events, bare-stream, and bare-structured-clone (see Bare modules).

See also

On this page