I have been trying to send messages to clients from service worker but if I use
self.clients.matchAll()
.then((clients) => {
clients.forEach(function(client) {
client.postMessage({msg: 'Hello from SW'})
})
})
it won't send to any client even if I have a tab open in the browser, but if I send a message from the client to the service worker
// client
navigator.serviceWorker.controller.postMessage({title: 'Send message from client'})
and in the service worker
self.addEventListener('message', function(event) {
self.clients.fetchAll()
.then((clients) => {
clients.forEach(function(client) {
client.postMessage({msg: 'Hello from SW'})
})
})
})
it can send a message and finds clients. what am I doing wrong?, should I use indexedDB instead?