--- url: "https://web.dev/articles/two-way-communication-guide" titulek: "Two-way communication with service workers | Articles | web.dev" h1: "Two-way communication with service workers Stay organized with collections Save and categorize content based on your preferences." popis: "How establish a two-way communication channel between the page and the service worker." jazyk: "en" kanonicka: "https://web.dev/articles/two-way-communication-guide" drobecky: ["Articles", "Two-way communication with service workers"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 00:03:42" znaku: 12685 slov: 1618 kodovani: "iso-8859-2" sha256: "c07633f15be590fab467510749f964b6e4e5b9f546a1bd4e1fb194624be92163" --- Two-way communication with service workers Stay organized with collections Save and categorize content based on your preferences. Andrew Guan DemiĂĄn Renzulli In some cases, a web app might need to establish a **two-way** communication channel between the page and the service worker. For example: in a podcast PWA one could build a feature to let the user [download episodes for offline consumption](/articles/app-like-pwas#proactive_background_downloading) and allow the service worker to keep the page regularly informed about the progress, so the [main thread](https://developer.mozilla.org/docs/Glossary/Main_thread) can update the UI. In this guide we'll explore the different ways of implementing a **two-way** communication between the [Window](https://developer.mozilla.org/docs/Web/API/Window) and [service worker](https://developer.mozilla.org/docs/Web/API/Service_Worker_API) context, by exploring different APIs, the [Workbox library](https://developer.chrome.com/docs/workbox), as well as some advanced cases. Using Workbox [`workbox-window`](https://developer.chrome.com/docs/workbox/modules/workbox-window) is a set of modules of the [Workbox library](https://developer.chrome.com/docs/workbox) that are intended to run in the window context. The [`Workbox`](https://developer.chrome.com/docs/workbox/reference/workbox-window#type-Workbox) class provides a `messageSW()` method to send a message to the instance's registered service worker and await a response. The following page code creates a new `Workbox` instance and sends a message to the service worker to obtain its version: `const wb = new Workbox('/sw.js'); wb.register(); const swVersion = await wb.messageSW({type: 'GET_VERSION'}); console.log('Service Worker version:', swVersion);` The service worker implements a message listener on the other end, and responds to the registered service worker: `const SW_VERSION = '1.0.0'; self.addEventListener('message', (event) => { if (event.data.type === 'GET_VERSION') { event.ports[0].postMessage(SW_VERSION); } });` Under the hood the library uses a browser API that we'll review in the next section: [Message Channel](https://developer.mozilla.org/docs/Web/API/Channel_Messaging_API), but abstracts many implementation details, making it easier to use, while leveraging the [wide browser support](https://caniuse.com/mdn-api_messagechannel_port1) this API has. Using Browser APIs If the Workbox library is not enough for your needs, there are several lower-level APIs available to implement **"two-way"** communication between pages and service workers. They have some similarities and differences: Similarities: In all cases the communication starts on one end via the `postMessage()` interface and is received on the other end by implementing a `message` handler. In practice, all the available APIs allow us to implement the same use cases, but some of them might simplify development in some scenarios. Differences: They have different ways of identifying the other side of the communication: some of them use an explicit reference to the other context, while others can communicate implicitly via a proxy object instantiated on each side. Browser support varies among them. Broadcast Channel API Browser Support 54 79 38 15.4 [Source](https://developer.mozilla.org/docs/Web/API/BroadcastChannel) The [Broadcast Channel API](https://developer.mozilla.org/docs/Web/API/Broadcast_Channel_API) allows basic communication between browsing contexts via [BroadcastChannel objects](https://developer.mozilla.org/docs/Web/API/BroadcastChannel). To implement it, first, each context has to instantiate a `BroadcastChannel` object with the same ID and send and receive messages from it: `const broadcast = new BroadcastChannel('channel-123');` The BroadcastChannel object exposes a `postMessage()` interface to send a message to any listening context: `//send message broadcast.postMessage({ type: 'MSG_ID', });` Any browser context can listen to messages via the `onmessage` method of the `BroadcastChannel` object: `//listen to messages broadcast.onmessage = (event) => { if (event.data && event.data.type === 'MSG_ID') { //process message... } };` As seen, there's no explicit reference to a particular context, so there's no need of obtaining a reference first to the service worker or any particular client. The disadvantage is that, at the moment of this writing, the API has support from Chrome, Firefox and Edge, but other browsers, like Safari, [don't support it yet](https://caniuse.com/?search=broadcastchannel). Client API Browser Support 40 17 44 11.1 [Source](https://developer.mozilla.org/docs/Web/API/Client) The [Client API](https://developer.mozilla.org/docs/Web/API/Client) allows you to obtain a reference to all the [`WindowClient`](https://developer.mozilla.org/docs/Web/API/WindowClient) objects representing the active tabs that the service worker is controlling. Since the page is controlled by a single service worker, it listens to and sends messages to the active service worker directly via the `serviceWorker` interface: `//send message navigator.serviceWorker.controller.postMessage({ type: 'MSG_ID', }); //listen to messages navigator.serviceWorker.onmessage = (event) => { if (event.data && event.data.type === 'MSG_ID') { //process response } };` Similarly, the service worker listens to messages by implementing an `onmessage` listener: `//listen to messages self.addEventListener('message', (event) => { if (event.data && event.data.type === 'MSG_ID') { //Process message } });` To communicate back with any of its clients, the service worker obtains an array of [`WindowClient`](https://developer.mozilla.org/docs/Web/API/WindowClient) objects by executing methods such as [`Clients.matchAll()`](https://developer.mozilla.org/docs/Web/API/Clients/matchAll) and [`Clients.get()`](https://developer.mozilla.org/docs/Web/API/Clients/get). Then it can `postMessage()` any of them: `//Obtain an array of Window client objects self.clients.matchAll(options).then(function (clients) { if (clients && clients.length) { //Respond to last focused tab clients[0].postMessage({type: 'MSG_ID'}); } });` `Client API` is a good option to communicate easily with all the active tabs from a service worker in a relatively straightforward way. The API is supported by [all major browsers](https://wpt.fyi/results/service-workers/service-worker/client-id.https.html?label=experimental&label=master&aligned), but not all its methods might be available, so make sure to check browser support before implementing it in your site. Message Channel Browser Support 2 12 41 5 [Source](https://developer.mozilla.org/docs/Web/API/MessageChannel) [Message Channel](https://developer.mozilla.org/docs/Web/API/Channel_Messaging_API) requires defining and passing a port from one context to another to establish a **two-way** communication channel. To initialize the channel, the page instantiates a [`MessageChannel`](https://developer.mozilla.org/docs/Web/API/MessageChannel/MessageChannel) object and uses it to send a port to the registered service worker. The page also implements an `onmessage` listener on it to receive messages from the other context: `const messageChannel = new MessageChannel(); //Init port navigator.serviceWorker.controller.postMessage({type: 'PORT_INITIALIZATION'}, [ messageChannel.port2, ]); //Listen to messages messageChannel.port1.onmessage = (event) => { // Process message };` The service worker receives the port, saves a reference to it and uses it to send a message to the other side: `let communicationPort; //Save reference to port self.addEventListener('message', (event) => { if (event.data && event.data.type === 'PORT_INITIALIZATION') { communicationPort = event.ports[0]; } }); //Send messages communicationPort.postMessage({type: 'MSG_ID'});` `MessageChannel` is currently supported by [all major browsers](https://caniuse.com/?search=channel). Advanced APIs: Background Sync and Background Fetch In this guide we explored ways of implementing **two-way** communication techniques, for relatively simple cases, like passing a string message describing the operation to perform, or a list of URLs to cache from one context to the other. In this section we'll explore two APIs to handle specific scenarios: lack of connectivity and long downloads. Background Sync Browser Support 49 79 x x [Source](https://developer.mozilla.org/docs/Web/API/SyncManager) A chat app might want to make sure that messages are never lost due to bad connectivity. The [Background Sync API](https://developer.chrome.com/blog/background-sync) lets you defer actions to be retried when the user has stable connectivity. This is useful for ensuring that whatever the user wants to send, is actually sent. Instead of the `postMessage()` interface, the page registers a `sync`: `navigator.serviceWorker.ready.then(function (swRegistration) { return swRegistration.sync.register('myFirstSync'); });` The service worker then listens for the `sync` event to process the message: `self.addEventListener('sync', function (event) { if (event.tag == 'myFirstSync') { event.waitUntil(doSomeStuff()); } });` The function `doSomeStuff()` should return a promise indicating the success/failure of whatever it's trying to do. If it fulfills, the sync is complete. If it fails, another sync will be scheduled to retry. Retry syncs also wait for connectivity, and employ an exponential back-off. Once the operation has been performed, the service worker can then communicate back with the page to update the UI, by using any of the communication APIs explored earlier. Google search uses Background Sync to persist failed queries due to bad connectivity, and retry them later when the user is online. Once the operation is performed, they communicate the result to the user via a web push notification: Background Fetch Browser Support 74 79 x x [Source](https://developer.mozilla.org/docs/Web/API/BackgroundFetchManager) For relatively short bits of work like sending a message, or a list of URLs to cache, the options explored so far are a good choice. If the task takes too long the browser will kill the service worker, otherwise it's a risk to the user's privacy and battery. The [Background Fetch API](https://developer.chrome.com/blog/background-fetch) allows you to offload a long task to a service worker, like downloading movies, podcasts, or levels of a game. To communicate to the service worker from the page, use `backgroundFetch.fetch`, instead of `postMessage()`: `navigator.serviceWorker.ready.then(async (swReg) => { const bgFetch = await swReg.backgroundFetch.fetch( 'my-fetch', ['/ep-5.mp3', 'ep-5-artwork.jpg'], { title: 'Episode 5: Interesting things.', icons: [ { sizes: '300x300', src: '/ep-5-icon.png', type: 'image/png', }, ], downloadTotal: 60 * 1024 * 1024, }, ); });` The `BackgroundFetchRegistration` object allows the page listen to the `progress` event to follow the progress of the download: `bgFetch.addEventListener('progress', () => { // If we didn't provide a total, we can't provide a %. if (!bgFetch.downloadTotal) return; const percent = Math.round( (bgFetch.downloaded / bgFetch.downloadTotal) * 100, ); console.log(`Download progress: ${percent}%`); });` The UI is updated to indicate the progress of a download (left). Thanks to service workers, the operation can continue running when all tabs have been closed (right). Next steps In this guide we explored the most general case of communication between page and service workers (bidirectional communication). Many times, one might need only one context to communicate with the other, without receiving a response. Check out the following guides for guidance how to implement unidirectional techniques in your pages from and to the service worker, along with use cases and production examples: [Imperative caching guide](/articles/imperative-caching-guide): Calling a service worker from the page to cache resources in advance (e.g. in prefetching scenarios). [Broadcast updates](/articles/broadcast-updates-guide): Calling the page from the service worker to inform about important updates (e.g. a new version of the webapp is available). Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2020-12-08 UTC.