You are viewing the preview version of this book
Click here for the full version.

Using async functions with postMessage

The postMessage call allows an asynchronous communication channel between different browsing contexts, such as with IFrames and web workers, where direct function calls don't work. It works by sending a message to the other side, then the receiving end can listen to message events.

For example, the page can communicate with an IFrame via postMessage and send events to each others' windows. The iframe.contentWindow.postMessage() call sends a message to the IFrame, while the window.parent.postMessage() sends a message back to the main page. The two ends can listen to messages from the other using window.addEventListener("message", (event) => {...}).

// index.html
const iframe = document.querySelector("iframe");

window.addEventListener("message", ({data}) => {
  // 3: receive response
  console.log("Message from iframe: " + data);

// 1: send request
iframe.contentWindow.postMessage([5, 2]);

// iframe.html
window.addEventListener("message", ({data}) => {
  // 2: send response
  window.parent.postMessage([0] +[1]);
page-IFrame communication

For web workers, each worker has a separate message handler. The page can send a message to a specific worker using the worker.postMessage() call and listen for events from that worker using worker.addEventListener("message", (event) => {...}). On the other side, the worker sends and receives events using the global functions postMessage() and addEventListener():

// index.html
const worker = new Worker("worker.js");
worker.addEventListener("message", ({data}) => {
  console.log("Message from worker: " + data); // 3

worker.postMessage([5, 5]); // 1

// worker.js
addEventListener("message", (event) => {
  postMessage([0] +[1]); // 2
}, false)

Request-response communication

There is more, but you've reached the end of this preview
Read this and all other chapters in full and get lifetime access to:
  • all future updates
  • full web-based access
  • PDF and Epub versions