LogoPear Docs
ReferencesBareModules

bare-channel

Reference for bare-channel: point-to-point inter-thread messaging for Bare, with transferable handles and stream interfaces.

stable

bare-channel provides point-to-point messaging between Bare threads. A channel exposes a transferable handle; pass it to another thread, reconstruct with Channel.from(handle), and exchange structured-cloned values over a port. It's a native addon and requires Bare >=1.7.0. For one-to-many fan-out, see bare-broadcast-channel.

npm i bare-channel

Usage

const Channel = require('bare-channel')
const { Thread } = Bare

const channel = new Channel()

new Thread(__filename, { data: channel.handle }, async (handle) => {
  const Channel = require('bare-channel')
  const port = Channel.from(handle).connect()
  console.log(await port.read())
  await port.close()
})

const port = channel.connect()
await port.write('hello')

API

Channel

const channel = new Channel([options]) · Channel.from(handle[, options])

Create a channel, or reconstruct one from a transferred channel.handle. channel.interfaces lists its interfaces; channel.connect() returns a Port.

Port

await port.write(value[, options]) · port.writeSync(value[, options])

Send a structured-cloneable value; returns whether the write flushed.

const data = await port.read() · port.readSync()

Read the next value. A port is also iterable (for await (const data of port)).

port.createReadStream([options]) · port.createWriteStream([options]) · port.createStream([options])

Stream interfaces over the port.

port.ref() · port.unref() · await port.close()

Event-loop references and teardown. Ports emit end and close.

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

See also

On this page