Workers

port.postMessage()

Sends a structured-cloned value. Transferring an owned ArrayBuffer detaches it at the sender; do not transfer pooled Buffer storage indiscriminately. Tested on Node.js 22.16.0. Example output (one line per entry): ["0 9 8"].

Formal Syntax & Structure

nodejs syntaxnodejs
port.postMessage(value[, transferList])

Parameters & Attributes

NameTypeDescription
valueunknownCloneable payload.
transferListobject[]Transferable objects.

Return Value

undefined

Queues a message.

Working Example

Example Snippetnodejs
import { MessageChannel } from 'node:worker_threads';import { once } from 'node:events';const { port1, port2 } = new MessageChannel();try {    const bytes = new Uint8Array([9, 8]);    const received = once(port2, 'message');    port1.postMessage(bytes, [bytes.buffer]);    const [copy] = await received;    console.log(bytes.byteLength, copy[0], copy[1]);}finally {    port1.close();    port2.close();}
Node.js Reference