HTML5 Web Messaging
Web messaging is used to deliver messages between documents. Web messaging allows cross-origin resource sharing (CORS).
With the Web Messaging API, messages are sent and received:
- Using the MessageEvent object
- Asynchronously
- Within 1 domain or between different domains
Cross-document Messaging
You can send and receive data between more than 2 Web pages. Because the Same Origin Policy is not applied for cross-document messaging, communication between other domains is also possible.
Send the message from the sending page using the postMessage() method of the receiving page window object. To receive the page, the receiving page window object must be registered to receive messages.
The postMessage() method supports the following parameters:
- message: Message to be sent.
- targetOrigin: Domain receiving the message. If a certain domain cannot be defined, use the wildcard ('*').
- transfer (optional): List of transferable objects.
Channel Messaging
With channel messaging, you can send and receive messages through the port of the MessageChannel interface.
The MessageChannel instance broadcasts message sending and receiving, and has 2 properties: port1 and port2. Each port is used to send and receive messages, and a message that is sent from one port with the postMessage() method is received by the other through the message event.