MessageChannel
Summary
Creates a connection (two “entangled” ports) to enable independent pieces of code (running in different browsing contexts) to communicate directly.
Properties
- port1
- Returns the first MessagePort object of the MessageChannel.
- port2
- Returns the second MessagePort object.
Methods
No methods.
Events
No events.
Examples
var msgChannel = new MessageChannel();
Notes
When you create a new MessageChannel object, it has two connected MessagePort objects (port1 and port2). One of the ports is sent to another window or frame, and messages can be sent and received without the repeated origin checking that is needed when using window.postMessage. Validation of the origin of a port and messages need only be done when a port is sent to windows other than the one that created them. MessagePort simplifies the messaging process by sending and receiving messages through two (and only those two) connected ports.
Messages are posted between the ports using postMessage. As the ports will only accept messages between the connected ports, no further validation is required once the connection is established. MessageChannel enables asynchronous communication between IFrameElements, cross-domain windows, or same page communications.
Related specifications
- W3C Web Messaging Specification
- W3C Candidate Recommendation
Attributions
Microsoft Developer Network: Windows Internet Explorer API reference Article