postMessage()
The method used to send data between a page and a worker, or between windows, delivered as a message event on the other side.
In more detail
The data is copied with the structured clone algorithm, unless you list transferable objects to move instead. Service workers also use postMessage to tell pages a new version is ready.
Related terms
- Web Worker — A script that runs on its own background thread, separate from the page's main thread, so heavy computation does not freeze the page.
- structured clone algorithm — The standard JavaScript algorithm for deep-copying values between threads and into storage, which handles Maps, Sets, Dates, typed arrays and circular references.
- transferable object — An object, such as an ArrayBuffer, ImageBitmap or MessagePort, whose memory can be moved to another thread instead of copied.
- MessagePort — One end of a two-way message channel created with MessageChannel, which can be transferred so two contexts talk directly.
Taught in
- Web Workers: Off-Thread Computation And Structured Clone — Frontend Architecture & Web Platform
- Service Worker Lifecycle: Installation, Activation And Update — Frontend Architecture & Web Platform