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

Node.js · Class

MessageChannel

MessageChannel creates an asynchronous, bidirectional communication channel in Node.js. Its two linked MessagePort endpoints let code exchange messages within a thread or across workers.

Since
v10.5.0

How it works

Constructing a MessageChannel creates two linked MessagePort objects. Messages posted through either port can be received asynchronously by the other, allowing communication within a thread or after transferring a port to another worker.

Syntax

new MessageChannel()

Returns

A MessageChannel object with port1 and port2 properties referencing linked MessagePort instances.

Key points

  • MessageChannel was introduced in Node.js v10.5.0.
  • It models an asynchronous channel that supports communication in both directions.
  • Its API surface is the pair of ports rather than methods defined directly on the channel.
  • Construction returns an object exposing two endpoint properties named port1 and port2.
  • The endpoints are linked MessagePort instances that form the channel.
  • Use it when a distinct, explicitly managed communication path is useful between workers or components.

Common mistakes

  • Do not look for messaging methods on MessageChannel itself; use its port1 and port2 MessagePort objects.
  • The two ports are paired endpoints, so handlers and postMessage calls belong on the ports rather than on the channel.

Example: Send a message through a channel

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 two linked ports, sends a structured value through one, and receives it on the other.

When to use it

Separate message routes

Create a private communication path between two cooperating parts of an application.

Worker-to-worker communication

Connect worker threads using a dedicated pair of transferable endpoints.

Structured asynchronous messaging

Exchange structured values asynchronously without sharing a single default message route.

Interview questions

QWhat does MessageChannel provide?

MessageChannel creates an asynchronous, two-way communication channel. Its constructor returns port1 and port2, which are linked MessagePort instances used to send and receive messages.

Follow-up: How would you connect separate workers? Transfer one of the channel's MessagePort objects to another worker through an existing communication channel, then communicate over the transferred port.

QWhere do you send messages after constructing a MessageChannel?

It has no methods of its own. The channel exposes two linked ports, and each port provides the operational messaging interface.

Follow-up: Why create a custom channel instead of relying only on a worker's default channel? A custom channel separates communication paths and responsibilities between components.

QHow is a MessageChannel instantiated and accessed in Node.js?

Import MessageChannel from node:worker_threads and construct it with new MessageChannel(). The resulting port1 and port2 properties identify the linked endpoints.

Follow-up: What kind of data can the endpoints exchange? The related MessagePort API supports structured data, memory regions, and other MessagePort objects, subject to its transfer rules.

  • MessagePort
  • Worker
  • parentPort
  • worker_threads

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