Skip to content
uencoder

MessageChannel

MessageChannel creates a pair of coupled MessagePorts whose ends communicate bidirectionally via port.postMessage and can be passed across origins.

Purpose

To provide a bidirectional asynchronous communication channel for Workers, iframes, and cross-context messaging.

Mechanism

new MessageChannel() creates port1 and port2; one side holds port1 and the other port2 (transferred via postMessage). Each port receives via onmessage and sends via postMessage.

Use cases

  • Worker bidirectional communication
  • iframe communication
  • Asynchronous message channels

Related tools

References