ServiceWorkerContainer: message event - Web APIs | MDN Skip to main content Skip to search MDN HTML HTML: Markup language HTML reference Elements Global attributes Attributes See all… HTML guides Responsive images HTML cheatsheet Date & time formats See all… Markup languages SVG MathML XML CSS CSS: Styling language CSS reference Properties Selectors At-rules Values See all… CSS guides Box model Animations Flexbox Colors See all… Layout cookbook Column layouts Centering an element Card component See all… JavaScriptJS JavaScript: Scripting language JS reference Standard built-in objects Expressions & operators Statements & declarations Functions See all… JS guides Control flow & error handing Loops and iteration Working with objects Using classes See all… Web APIs Web APIs: Programming interfaces Web API reference File system API Fetch API Geolocation API HTML DOM API Push API Service worker API See all… Web API guides Using the Web animation API Using the Fetch API Working with the History API Using the Web speech API Using web workers All All web technology Technologies Accessibility HTTP URI Web extensions WebAssembly WebDriver See all… Topics Media Performance Privacy Security Progressive web apps Learn Learn web development Frontend developer course Getting started modules Core modules MDN Curriculum Check out the video course from Scrimba, our partner Learn HTML Structuring content with HTML module Learn CSS CSS styling basics module CSS layout module Learn JavaScript Dynamic scripting with JavaScript module Tools Discover our tools Playground HTTP Observatory Border-image generator Border-radius generator Box-shadow generator Color format converter Color mixer Shape generator About Get to know MDN better About MDN Advertise with us Community MDN on GitHub Blog Toggle sidebar Web Web APIs ServiceWorkerContainer message Theme OS default Light Dark English (US) Remember language Learn more Deutsch English (US) 日本語 ServiceWorkerContainer: message event Baseline Widely available This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2018. Learn more See full compatibility Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers. Note: This feature is available in Web Workers. The message event is used in a page controlled by a service worker to receive messages from the service worker. This event is not cancelable and does not bubble. In this article Syntax Event type Event properties Examples Specifications Browser compatibility See also Syntax Use the event name in methods like addEventListener(), or set an event handler property. js addEventListener("message", (event) => { }) onmessage = (event) => { } Event type A MessageEvent. Inherits from Event. Event MessageEvent Event properties This interface also inherits properties from its parent, Event. MessageEvent.data Read only The data sent by the message emitter. MessageEvent.origin Read only A string representing the origin of the message emitter. MessageEvent.lastEventId Read only A string representing a unique ID for the event. MessageEvent.source Read only A MessageEventSource (which can be a WindowProxy, MessagePort, or ServiceWorker object) representing the message emitter. MessageEvent.ports Read only An array of MessagePort objects representing the ports associated with the channel the message is being sent through (where appropriate, e.g., in channel messaging or when sending a message to a shared worker). Examples In this example the service worker get the client's ID from a fetch event and then sends it a message using Client.postMessage: js // service-worker.js async function messageClient(clientId) { const client = await clients.get(clientId); client.postMessage("Hi client!"); } addEventListener("fetch", (event) => { messageClient(event.clientId); event.respondWith(() => { // … }); }); The client can receive the message by listening to the message event: js // main.js navigator.serviceWorker.addEventListener("message", (message) => { console.log(message); }); Alternatively, the client can receive the message with the onmessage event handler: js // main.js navigator.serviceWorker.onmessage = (message) => { console.log(message); }; Specifications Specification Service Workers Nightly # dom-serviceworkercontainer-onmessage Browser compatibility Enable JavaScript to view this browser compatibility table. See also Using Service Workers Service workers basic code example Using web workers Help improve MDN Was this page helpful to you? Yes No Learn how to contribute This page was last modified on May 2, 2025 by MDN contributors. View this page on GitHub • Report a problem with this content Filter sidebar Clear filter input Service Worker API ServiceWorkerContainer Instance properties controller ready Instance methods getRegistration() getRegistrations() register() startMessages() Events controllerchange message messageerror Inheritance EventTarget Related pages for Service Workers API Cache CacheStorage Client Clients ExtendableEvent ExtendableMessageEvent FetchEvent InstallEvent NavigationPreloadManager Navigator.serviceWorker ServiceWorker ServiceWorkerGlobalScope ServiceWorkerRegistration Window.caches WindowClient WorkerGlobalScope.caches WorkerNavigator.serviceWorker Guides Using Service Workers MDN Your blueprint for a better internet. MDN About Blog Mozilla careers Advertise with us MDN Plus Product help Contribute MDN Community Community resources Writing guidelines MDN Discord MDN on GitHub Developers Web technologies Learn web development Guides Tutorials Glossary Hacks blog Mozilla Website Privacy Notice Telemetry Settings Legal Community Participation Guidelines Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.