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();
}
Constructors Jump to heading
new
Jump to headingBroadcastChannel(name: string)
Properties Jump to heading
readonly
Jump to headingname: string
Jump to headingonmessage: (message: unknown) => void
Invoked with a single `MessageEvent` argument when a message is received.
Jump to headingonmessageerror: (message: unknown) => void
Invoked with a received message cannot be deserialized.
Methods Jump to heading
Jump to headingclose(): void
Closes the BroadcastChannel
connection.
Jump to headingpostMessage(message: unknown): void