Workers

new MessageChannel()

Creates two connected message ports. Close both owned endpoints once communication is complete. Tested on Node.js 22.16.0. Example output (one line per entry): ["7"].

Formal Syntax & Structure

nodejs syntaxnodejs
new MessageChannel()

Return Value

MessageChannel

Pair of connected ports.

Working Example

Example Snippetnodejs
import { MessageChannel } from 'node:worker_threads';import { once } from 'node:events';const { port1, port2 } = new MessageChannel();try {    const received = once(port2, 'message');    port1.postMessage({ id: 7 });    console.log((await received)[0].id);}finally {    port1.close();    port2.close();}
Node.js Reference