MessagePort
History
EventTarget rather than from EventEmitter.class MessagePort extends EventTarget
Instances of the worker.MessagePort class represent one end of an
asynchronous, two-way communications channel. It can be used to transfer
structured data, memory regions and other MessagePorts between different
Workers.
This implementation matches browser MessagePorts.
The 'close' event is emitted once either side of the channel has been
disconnected.
import { MessageChannel } from 'node:worker_threads'; const { port1, port2 } = new MessageChannel(); // Prints: // foobar // closed! port2.on('message', (message) => console.log(message)); port2.once('close', () => console.log('closed!')); port1.postMessage('foobar'); port1.close();
const { MessageChannel } = require('node:worker_threads'); const { port1, port2 } = new MessageChannel(); // Prints: // foobar // closed! port2.on('message', (message) => console.log(message)); port2.once('close', () => console.log('closed!')); port1.postMessage('foobar'); port1.close();
anyThe 'message' event is emitted for any incoming message, containing the cloned
input of port.postMessage().
Listeners on this event receive a clone of the value parameter as passed
to postMessage() and no further arguments.
ErrorThe 'messageerror' event is emitted when deserializing a message failed.
Currently, this event is emitted when there is an error occurring while
instantiating the posted JS object on the receiving end. Such situations
are rare, but can happen, for instance, when certain Node.js API objects
are received in a vm.Context (where Node.js APIs are currently
unavailable).
port.close(): void
Disables further sending of messages on either side of the connection.
This method can be called when no further communication will happen over this
MessagePort.
The 'close' event is emitted on both MessagePort instances that
are part of the channel.
port.postMessage
History
X509Certificate to the list of cloneable types.CryptoKey to the list of cloneable types.KeyObject to the list of cloneable types.FileHandle to the list of transferable types.port.postMessage(value, transferList?): void
Sends a JavaScript value to the receiving side of this channel.
value is transferred in a way which is compatible with
the HTML structured clone algorithm.
In particular, the significant differences to JSON are:
valuemay contain circular references.valuemay contain instances of builtin JS types such asRegExps,BigInts,Maps,Sets, etc.valuemay contain typed arrays, both usingArrayBuffers andSharedArrayBuffers.valuemay containWebAssembly.Moduleinstances.valuemay not contain native (C++-backed) objects other than:Attributess (TCP only, when listed intransferList),s (TCP only, when listed intransferList),
import { MessageChannel } from 'node:worker_threads'; const { port1, port2 } = new MessageChannel(); port1.on('message', (message) => console.log(message)); const circularData = {}; circularData.foo = circularData; // Prints: { foo: [Circular] } port2.postMessage(circularData);
const { MessageChannel } = require('node:worker_threads'); const { port1, port2 } = new MessageChannel(); port1.on('message', (message) => console.log(message)); const circularData = {}; circularData.foo = circularData; // Prints: { foo: [Circular] } port2.postMessage(circularData);
transferList may be a list of ArrayBuffer, MessagePort,
FileHandle, net.Server, and net.Socket objects.
After transferring, they are not usable on the sending side of the channel
anymore (even if they are not contained in value).
Transferring a net.Server moves its listening socket — together with any
pending connections in the accept queue — to the receiving thread's event loop.
Transferring a net.Socket moves a single connection; the socket must be a
freshly accepted or created TCP connection that has not yet started reading and
has no buffered data, otherwise postMessage() throws
ERR_WORKER_HANDLE_NOT_TRANSFERABLE. This makes it possible to accept
connections on one thread and distribute them across a pool of worker threads.
Only TCP handles are supported.
If value contains SharedArrayBuffer instances, those are accessible
from either thread. They cannot be listed in transferList.
value may still contain ArrayBuffer instances that are not in
transferList; in that case, the underlying memory is copied rather than moved.
import { MessageChannel } from 'node:worker_threads'; const { port1, port2 } = new MessageChannel(); port1.on('message', (message) => console.log(message)); const uint8Array = new Uint8Array([ 1, 2, 3, 4 ]); // This posts a copy of `uint8Array`: port2.postMessage(uint8Array); // This does not copy data, but renders `uint8Array` unusable: port2.postMessage(uint8Array, [ uint8Array.buffer ]); // The memory for the `sharedUint8Array` is accessible from both the // original and the copy received by `.on('message')`: const sharedUint8Array = new Uint8Array(new SharedArrayBuffer(4)); port2.postMessage(sharedUint8Array); // This transfers a freshly created message port to the receiver. // This can be used, for example, to create communication channels between // multiple `Worker` threads that are children of the same parent thread. const otherChannel = new MessageChannel(); port2.postMessage({ port: otherChannel.port1 }, [ otherChannel.port1 ]);
const { MessageChannel } = require('node:worker_threads'); const { port1, port2 } = new MessageChannel(); port1.on('message', (message) => console.log(message)); const uint8Array = new Uint8Array([ 1, 2, 3, 4 ]); // This posts a copy of `uint8Array`: port2.postMessage(uint8Array); // This does not copy data, but renders `uint8Array` unusable: port2.postMessage(uint8Array, [ uint8Array.buffer ]); // The memory for the `sharedUint8Array` is accessible from both the // original and the copy received by `.on('message')`: const sharedUint8Array = new Uint8Array(new SharedArrayBuffer(4)); port2.postMessage(sharedUint8Array); // This transfers a freshly created message port to the receiver. // This can be used, for example, to create communication channels between // multiple `Worker` threads that are children of the same parent thread. const otherChannel = new MessageChannel(); port2.postMessage({ port: otherChannel.port1 }, [ otherChannel.port1 ]);
The message object is cloned immediately, and can be modified after posting without having side effects.
For more information on the serialization and deserialization mechanisms
behind this API, see the serialization API of the node:v8 module.
All TypedArray | Buffer instances are views over an underlying
ArrayBuffer. That is, it is the ArrayBuffer that actually stores
the raw data while the TypedArray and Buffer objects provide a
way of viewing and manipulating the data. It is possible and common
for multiple views to be created over the same ArrayBuffer instance.
Great care must be taken when using a transfer list to transfer an
ArrayBuffer as doing so causes all TypedArray and Buffer
instances that share that same ArrayBuffer to become unusable.
const ab = new ArrayBuffer(10); const u1 = new Uint8Array(ab); const u2 = new Uint16Array(ab); console.log(u2.length); // prints 5 port.postMessage(u1, [u1.buffer]); console.log(u2.length); // prints 0
For Buffer instances, specifically, whether the underlying
ArrayBuffer can be transferred or cloned depends entirely on how
instances were created, which often cannot be reliably determined.
An ArrayBuffer can be marked with markAsUntransferable() to indicate
that it should always be cloned and never transferred.
Depending on how a Buffer instance was created, it may or may
not own its underlying ArrayBuffer. An ArrayBuffer must not
be transferred unless it is known that the Buffer instance
owns it. In particular, for Buffers created from the internal
Buffer pool (using, for instance Buffer.from() or Buffer.allocUnsafe()),
transferring them is not possible and they are always cloned,
which sends a copy of the entire Buffer pool.
This behavior may come with unintended higher memory
usage and possible security concerns.
See Buffer.allocUnsafe() for more details on Buffer pooling.
The ArrayBuffers for Buffer instances created using
Buffer.alloc() or Buffer.allocUnsafeSlow() can always be
transferred but doing so renders all other existing views of
those ArrayBuffers unusable.
Because object cloning uses the HTML structured clone algorithm,
non-enumerable properties, property accessors, and object prototypes are
not preserved. In particular, Buffer objects will be read as
plain Uint8Arrays on the receiving side, and instances of JavaScript
classes will be cloned as plain JavaScript objects.
const b = Symbol('b'); class Foo { #a = 1; constructor() { this[b] = 2; this.c = 3; } get d() { return this.#a + 3; } } const { port1, port2 } = new MessageChannel(); port1.onmessage = ({ data }) => console.log(data); port2.postMessage(new Foo()); // Prints: { c: 3 }
Some built-in objects cannot be cloned at all. For example, posting a
URL object throws a DataCloneError:
const { port1, port2 } = new MessageChannel(); port2.postMessage(new URL('https://example.org')); // Throws DataCloneError: Cannot clone object of unsupported type.
port.hasRef
History
port.hasRef(): boolean
booleanIf true, the MessagePort object will keep the Node.js event loop active.
port.ref(): void
Opposite of unref(). Calling ref() on a previously unref()ed port does
not let the program exit if it's the only active handle left (the default
behavior). If the port is ref()ed, calling ref() again has no effect.
If listeners are attached or removed using .on('message'), the port
is ref()ed and unref()ed automatically depending on whether
listeners for the event exist.
port.start(): void
Starts receiving messages on this MessagePort. When using this port
as an event emitter, this is called automatically once 'message'
listeners are attached.
This method exists for parity with the Web MessagePort API. In Node.js,
it is only useful for ignoring messages when no event listener is present.
Node.js also diverges in its handling of .onmessage. Setting it
automatically calls .start(), but unsetting it lets messages queue up
until a new handler is set or the port is discarded.
port.unref(): void
Calling unref() on a port allows the thread to exit if this is the only
active handle in the event system. If the port is already unref()ed calling
unref() again has no effect.
If listeners are attached or removed using .on('message'), the port is
ref()ed and unref()ed automatically depending on whether
listeners for the event exist.