Can a service worker postMessage() directly to a shared worker client?

882 Views Asked by At

In a service worker, the client interface has postMessage() for talking to clients, which can be of type "window", "worker", or "sharedworker".

In "window" clients, the message event is sent to navigator.serviceWorker.controller. Example service worker code:

const windowClients = await clients.matchAll({ type: "window" });
windowClients[0].postMessage('Send this message to window.navigator.serviceWorker.controller');

But in shared worker clients, navigator.serviceWorker does not seem to exist. (This seems to change in a draft spec. Edit: And Safari.)

const sharedWorkerClients = await clients.matchAll({ type: "sharedworker" });
sharedWorkerClients[0].postMessage('Send this message to ???');

So where is the message event dispatched in the shared worker? Or is postMessage() a no-op here?

(There are workarounds like having the document transfer SharedWorker#port to the service worker. I'm wondering if there's a way to send messages directly.)

0

There are 0 best solutions below