Worker communication
Exchange messages between the main thread and a worker.
Node.js · Class
MessagePort is one endpoint of an asynchronous, two-way channel in Node.js worker_threads. It sends structured-clone data and can transfer memory regions or other MessagePort instances between workers.
A MessagePort is one endpoint of an asynchronous, bidirectional communication channel. Obtain linked ports from MessageChannel, register a message listener, and use postMessage() to send structured-clone data; ports can also be transferred between workers.
MessagePortconst { MessageChannel } = require('node:worker_threads');
const { port1, port2 } = new MessageChannel();
port1.once('message', (message) => {
console.log(message);
port1.close();
port2.close();
});
port2.postMessage({ status: 'ready' });Creates a linked pair of ports, sends structured data through one port, and receives it from the other.
Exchange messages between the main thread and a worker.
Create an in-process asynchronous communication channel with MessageChannel.
Pass transferable ports or memory-backed data between workers.
MessageChannelWorkerparentPortworker_threads