Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

communication with service worker

//one-way communication


// app.js - Somewhere in your web app
navigator.serviceWorker.controller.postMessage({
  type: 'MESSAGE_IDENTIFIER',
});
// service-worker.js
// On the Service Worker side we have to listen to the message event
self.addEventListener('message', (event) => {
  if (event.data && event.data.type === 'MESSAGE_IDENTIFIER') {
    // do something
  }
});

//two-way communication.


//Using the Broadcast API

// app.js
// Set up channel
const broadcast = new BroadcastChannel('count-channel');

// Listen to the response
broadcast.onmessage = (event) => {
  console.log(event.data.payload);
};

// Send first request
broadcast.postMessage({
  type: 'INCREASE_COUNT',
});

// service-worker.js
// Set up channel with same name as in app.js
const broadcast = new BroadcastChannel('count-channel');
broadcast.onmessage = (event) => {
  if (event.data && event.data.type === 'INCREASE_COUNT') {
    broadcast.postMessage({ payload: ++count });
  }
};

//Using the Client API

// app.js
// Listen to the response
navigator.serviceWorker.onmessage = (event) => {
  if (event.data && event.data.type === 'REPLY_COUNT_CLIENTS') {
    setCount(event.data.count);
  }
};

// Send first request
navigator.serviceWorker.controller.postMessage({
  type: 'INCREASE_COUNT_CLIENTS',
});

// service-worker.js
// Listen to the request
self.addEventListener('message', (event) => {
  if (event.data && event.data.type === 'INCREASE_COUNT') {
    // Select who we want to respond to
    self.clients.matchAll({
      includeUncontrolled: true,
      type: 'window',
    }).then((clients) => {
      if (clients && clients.length) {
        // Send a response - the clients
        // array is ordered by last focused
        clients[0].postMessage({
          type: 'REPLY_COUNT',
          count: ++count,
        });
      }
    });
  }
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: Navigation timeout of 30000 ms exceeded 
Javascript :: angular ng build Maximum call stack size exceeded 
Javascript :: mobile number regex javascript 
Javascript :: JS class for each 
Javascript :: web3 check if contract 
Javascript :: how to draw horizontal line in canvas 
Javascript :: html print div 
Javascript :: date methods js 
Javascript :: console.log regex 
Javascript :: TypeError: value.toLowerCase is not a function 
Javascript :: remove duplicate value from map react js 
Javascript :: how to get window size in react js 
Javascript :: kubectl get pod by node 
Javascript :: how to select a random value in a json array LUA 
Javascript :: why does hoisting does not work in function expressions 
Javascript :: reactjs compile subdomine 
Javascript :: date format in ngx-csv package in angular 
Javascript :: block enter key using jquery 
Javascript :: js change div content 
Javascript :: how to edit the link in a href with jquery 
Javascript :: how to clone a object in javascript angular 
Javascript :: remove multiple space to single javascript 
Javascript :: opal find element 
Javascript :: Configure morgan so that it also shows the data sent in HTTP POST requests: 
Javascript :: fill checkbox javascript 
Javascript :: js revers string fucntion 
Javascript :: how to format money as currency string 
Javascript :: Javascript noFill 
Javascript :: run a nodejs file infinite loop 
Javascript :: Do not know how to serialize a BigInt 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =