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.

options include:

options = {
  handle,
  interfaces: []
}

handle is an existing SharedArrayBuffer returned by channel.handle on another thread. When provided, the new channel is wired up to the same underlying channel. If omitted, a fresh channel is created.

interfaces is an array of constructors with bare-structured-clone serialize and/or transfer symbols. Instances of these types may be passed to port.write() and will be reconstructed on the receiving side.

channel.handle

The underlying SharedArrayBuffer for the channel. Pass this across thread boundaries (e.g. via Bare.Thread's data option) and reconstruct the channel on the other side with Channel.from(handle).

channel.interfaces

The array of constructors passed to the constructor.

const port = channel.connect()

Connect a new Port to the channel. A channel supports exactly two connected ports; messages written on one port appear in the read queue of the other.

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