Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Class

MessagePort

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.

How it works

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.

Syntax

MessagePort

Key points

  • A MessagePort represents one endpoint of an asynchronous, two-way communication channel.
  • Ports support transferring structured data, memory regions, and other MessagePort instances between workers.
  • MessagePort is provided by the node:worker_threads module, which supports parallel JavaScript execution.
  • MessageChannel creates the communication channel whose endpoints are MessagePort instances.

Example: Exchange a message through a MessageChannel

const { 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.

When to use it

Worker communication

Exchange messages between the main thread and a worker.

Thread-local messaging

Create an in-process asynchronous communication channel with MessageChannel.

Transfer communication resources

Pass transferable ports or memory-backed data between workers.

  • MessageChannel
  • Worker
  • parentPort
  • worker_threads

Source: Worker threads | Node.js 26.10.0 Documentation. Checked 2026-10-15.