Bun

class

worker_threads.BroadcastChannel

class BroadcastChannel

Instances of BroadcastChannel allow asynchronous one-to-many communication with all other BroadcastChannel instances bound to the same channel name.

'use strict';

import {
  isMainThread,
  BroadcastChannel,
  Worker,
} from 'node:worker_threads';

const bc = new BroadcastChannel('hello');

if (isMainThread) {
  let c = 0;
  bc.onmessage = (event) => {
    console.log(event.data);
    if (++c === 10) bc.close();
  };
  for (let n = 0; n < 10; n++)
    new Worker(__filename);
} else {
  bc.postMessage('hello from every worker');
  bc.close();
}
  • readonly name: string
  • onmessage: (message: unknown) => void

    Invoked with a single MessageEvent argument when a message is received.

  • onmessageerror: (message: unknown) => void

    Invoked with a received message cannot be deserialized.

  • close(): void;

    Closes the BroadcastChannel connection.

  • message: unknown
    ): void;
    @param message

    Any cloneable JavaScript value.

  • ref(): this;
  • unref(): this;