CKTIS / JavaScript
MessagePort — a messaging channel endpoint
MessagePort is one of the two endpoints of a bidirectional channel. It enables asynchronous delivery of data and transferable objects between connected pieces of code.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
const { port1, port2 } = new MessageChannel();
port1.postMessage(message);
port1.postMessage(message, transfer);
port1.postMessage(message, { transfer });
port2.addEventListener("message", handler);
port2.start();
port1.close();
port2.close();MessagePort is a Web API interface defined by HTML, not ECMAScript. You do not create it with new MessagePort(): new MessageChannel() supplies connected port1 and port2. A message sent through one port reaches the other as a message event; read its payload through event.data.
postMessage() queues delivery rather than calling the receiver synchronously. Data undergoes structured cloning. An optional transfer list transfers transferable objects instead of copying them; a port itself can also be transferred. The sender-side object then becomes unusable.
With addEventListener("message", handler), call start() on the receiving port. Assigning onmessage starts reception automatically. DataCloneError indicates a serialization or transfer-list error; messageerror signals failed deserialization at the receiver. Close ports with close() when communication is finished.
The example uses both ports within one page. This is a browser API; having Node 24 in a repository does not establish browser support.
Examples
(() => {
const { port1, port2 } = new MessageChannel();
port2.addEventListener("message", (event) => {
console.log(`received: ${event.data.value}`);
port1.close();
port2.close();
}, { once: true });
port2.start();
port1.postMessage({ value: 42 });
console.log("sent");
})();Result
sent received: 42
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.