This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The Clients
interface provides access to Client
objects. Access it via
within a service worker.self
.clients
Clients.get()
Promise
for a Client
matching a given id
.Clients.matchAll()
Promise
for an array of Client
objects. An options argument allows you to control the types of clients returned. Clients.openWindow()
Promise
for the new WindowClient
.Clients.claim()
controller
for all clients within its scope
. The following example shows an existing chat window or creates a new one when the user clicks a notification.
addEventListener('notificationclick', event => { event.waitUntil(async function() { const allClients = await clients.matchAll({ includeUncontrolled: true }); let chatClient; // Let's see if we already have a chat window open: for (const client of allClients) { const url = new URL(client.url); if (url.pathname == '/chat/') { // Excellent, let's use it! client.focus(); chatClient = client; break; } } // If we didn't find an existing chat window, // open a new one: if (!chatClient) { chatClient = await clients.openWindow('/chat/'); } // Message the client: chatClient.postMessage("New chat messages!"); }()); });
Specification | Status | Comment |
---|---|---|
Service Workers The definition of 'Clients' in that specification. | Working Draft | Initial definition |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 40 | ? | 44
|
No | 27 | No |
claim
|
42 | ? | 44
|
No | 29 | No |
get
|
51 | ? | 45
|
No | 38 | No |
matchAll
|
47
|
? | 44
|
No | 32 | No |
openWindow
|
40
|
? | 45
|
No | 38 | No |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 40 | 40 | ? | 44 | 27 | No | 4.0 |
claim
|
42 | 42 | ? | 44 | 29 | No | 4.0 |
get
|
No | 51 | ? | 45 | 38 | No | 5.0 |
matchAll
|
47
|
47
|
? | 44
|
32 | No | 4.0 |
openWindow
|
40
|
40
|
? | 45 | 38 | No | 4.0
|
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Clients