How postMessage() appears across browser context.
π΅ 1. iframe β iframe Communication
Classic one:
iframe.contentWindow.postMessage(message, targetOrigin)
- Sends from parent page into iframe.
- Or from iframe up to parent (
window.parent.postMessage
).
Spec:
π΅ 2. window β window Communication
Opening a new window and messaging it:
const popup = window.open('url');
popup.postMessage(message, targetOrigin);
- Parent β popup communication.
- Popup can send back using
window.opener.postMessage
.
π΅ 3. worker.postMessage() β Web Workers
Background worker threads:
const worker = new Worker('worker.js');
worker.postMessage({ foo: 'bar' });
Worker receives it in onmessage
.
Spec:
π΅ 4. SharedWorker.port.postMessage()
Multiple tabs sharing a worker:
const worker = new SharedWorker('shared.js');
worker.port.postMessage('hi');
(Uses MessagePort
under the hood.)
π΅ 5. ServiceWorker.postMessage()
Communicating with a Service Worker:
navigator.serviceWorker.controller.postMessage({ action: 'sync' });
Spec:
π΅ 6. BroadcastChannel.postMessage()
Channel to send messages across tabs and windows:
const channel = new BroadcastChannel('my_channel');
channel.postMessage('ping');
Spec:
π΅ 7. MessagePort.postMessage()
In MessageChannel or port.postMessage (underlying concept for transferable objects):
const { port1, port2 } = new MessageChannel();
port1.postMessage('hello');
π΅ 8. MediaDevices / MediaStreamTrack.postMessage (Indirectly via ports)
WebRTC / Media capture uses MessagePort
to control data flow internally.
π΅ 9. AudioWorkletNode.port.postMessage()
Low-level audio processing:
audioWorkletNode.port.postMessage({ frequency: 440 });
Spec:
π΅ 10. MessageEvent (receive side of postMessage)
Receiving postMessage
:
window.addEventListener('message', (event) => {
console.log(event.data);
});
Related to any postMessage
sender.
π΅ 11. WebRTC DataChannel.send() (similar to postMessage)
Not literally .postMessage()
, but similar semantics:
dataChannel.send('hello');
- Secure peer-to-peer messaging.
π΅ 12. navigator.presentation.receiver.connectionList.connections[x].postMessage()
Presentation API for screen sharing:
connection.postMessage('control');
π΅ 13. USBDevice.transferOut() (Data Transfer, postMessage pattern)
When sending bulk data to USB devices, the conceptual model is like postMessage
between contexts.
π΅ 14. WebSocket.send() (postMessage-like)
Again, not named postMessage, but same idea:
socket.send('hello server');
Data framed and delivered asynchronously.
π΅ 15. MessageChannel.postMessage() inside Service Worker to Client
Using clients.get()
β postMessage
back to client tab.
event.source.postMessage('response');
From Service Worker to client tab.
π΅ 16. IDBDatabase.postMessage()
Not directly, but when transferring structured clone data into IndexedDB transactions, the same serialization spec is used as postMessage
.
π΅ 17. PaymentRequest.postMessage()
Internally, the Payment API uses structured clone/postMessage patterns between the browser and payment handlers.
π΅ 18. Notification.postMessage() (future planned)
Push API proposals for interactive notifications included messaging channels.
π΅ 19. Worklet.postMessage()
Custom Worklets like CSSPaint or AudioWorklet use ports under the hood.
π΅ 20. WebAssembly.Module.postMessage (Transferable Object Proposals)
WASM module can be transferred between threads via postMessage
(experimental).
π΅ 21. Trusted Types Policy Violation Reporting via postMessage
Security policies can report violations asynchronously using postMessage patterns.
π΅ 22. WebGPU (GPUBuffer.postMessage proposal)
Future WebGPU
APIs plan to use transferable patterns for performance.
π΅ 23. WebTransport Streams
New QUIC-based messaging uses message framing very similar to postMessage semantics.
π΅ 24. Document.postMessage() (within same-origin documents)
Rare case: messaging between frames within same document tree.
π΅ 25. parent.postMessage() inside iframe
Iframe sending back up:
window.parent.postMessage('data', '*');
π΅ 26. opener.postMessage() inside child window
Popup sending back:
window.opener.postMessage('response', '*');
π΅ 27. window.top.postMessage()
Grandparent window messaging from deep iframes.
π΅ 28. ServiceWorkerClient.postMessage()
When handling push notifications:
client.postMessage('new data available');
π΅ 29. Extensions: chrome.runtime.sendMessage (internally postMessage)
Chrome Extensions API under the hood uses internal structured clone + postMessage:
chrome.runtime.sendMessage({ type: 'update' });
β Wrapped postMessage over extension messaging internals.
π΅ 30. Declarative Shadow DOM future messaging (proposals)
New APIs for messaging between declarative shadow roots might involve postMessage-like transfers.
π Full Technical Spec Source Paths
Context | Spec / Source |
---|---|
HTML Message Ports | https://html.spec.whatwg.org/multipage/web-messaging.html |
Worker Threads | https://html.spec.whatwg.org/multipage/workers.html |
Service Workers | https://w3c.github.io/ServiceWorker/ |
Audio Worklet | https://webaudio.github.io/web-audio-api/ |
BroadcastChannel | https://html.spec.whatwg.org/multipage/web-messaging.html#broadcasting-to-other-browsing-contexts |
MessageChannel | https://html.spec.whatwg.org/multipage/web-messaging.html#channel-messaging |
π Raw Browser Code Links (Chromium)
- content/browser/message_port/
- extensions/renderer/messaging_util.cc
- third_party/blink/renderer/core/messaging/
- content/browser/renderer_host/render_message_filter.cc
π Summary
β postMessage isnβt just iframe β parent.
β Itβs a core browser messaging backbone for:
- Iframes
- Tabs
- Workers
- Service Workers
- Broadcast channels
- Extensions
- Future APIs (WebGPU, WASM transfer, PaymentHandler, WebTransport)
β All use structured clone internally (safe serialization, copy-only no prototype pollution).
#
More information for research:
origin check flaws, MessageEvent sanitization, how structured clone serialization protects against prototype poisoning, and even attack surfaces inside Chromium/Blink for postMessage.