HTML5 Web Messaging

What is Web Messaging?

  • Web messaging is an API which was introduced in WHATWG HTML5 draft specification.
  • It allows the documents to communicate with one another across the different origins or the source domains.

Attributes

AttributesDescription
dataIt contains the string data.
originIt contains the domain name and the port.
lastEventIdIt contains a unique identifier for the current message event.
sourceIt contains a reference of the original document window.
portsIt contains the data that is sent by any message port.

Cross-document message

A new web browsing context is required to be created before a cross-document message is sent by creating a new iframe or window.

Data is sent by using postMessage() which has two arguments as follows:

message – the message that is required to be sent.
targetOrigin – the name of the origin.

Channel messaging

Channel messaging is a two way communication between the browsing context. They are used for communication across multiple origins.

MessageChannel and MessagePort Objects

There are two ports which are created internally while a messageChannel is created. The two ports are used to send data and forward it to another browsing context.

postMessage() - the throw channel posts the message.
start() - the data is sent.
close() -the ports are closed.