Name

MessageChannel — a pair of connected MessagePorts

Synopsis

A MessageChannel is simply a pair of connected MessagePort objects. Calling post Message() on either one triggers a message event on the other. If you want to establish a private communication channel with a Window or Worker thread, create a MessageChannel and then pass one member of the MessagePort pair to the Window or Worker (using the ports argument of postMessage()).

MessageChannel and MessagePort types are an advanced feature of HTML5 and, at the time of this writing, some browsers support cross-origin messaging (Cross-Origin Messaging) and worker threads (Web Workers) without supporting private communication channels with MessagePort.

Constructor

new MessageChannel()

This no-argument constructor returns a new MessageChannel object.

Properties

readonly MessagePort port1readonly MessagePort port2

These are the two connected ports that define the communication channel. The two are symmetrical: retain one or the other for your code, and pass the other to the Window or Worker you want to communicate with.

Get JavaScript: The Definitive Guide, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.