Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMLHttpRequest within service worker

I'm trying to create a push notification system on chrome. I have a php that fetches data from mysql and echoes a JSON, now I'd like to call a function getJsonCode() that it's activated when a push notification arrives and reads the JSON data.

Within my service worker I've created the standards functions. The problem is that when I create getJsonCode with a XMLHttpRequest it tells me it's not defined

self.addEventListener('install', function(event) {
  self.skipWaiting();
  console.log('Installed', event);
});
self.addEventListener('activate', function(event) {
  console.log('Activated', event);
});
self.addEventListener('push', function(event) {  
  console.log('Push message', event);
  getJsonCode();
  );
})

getJsonCode() {
  codeRequest= new XMLHttpRequest();
  codeRequest.open('get', 'myfileWithJson.php', true);
  codeRequest.send();
  dataJSON = codeRequest.responseText;
  console.log('rispostaJSON');
}

Is it possible to call an external file like this within a service worker or are there some kind of limitations? Because I don't get why this isn't working

like image 245
elisa tramonti Avatar asked May 09 '16 09:05

elisa tramonti


People also ask

How do you manipulate DOM using a service worker?

So what you need to do, is send message from service worker to document javascript with list of id/class/tagname/whatever of DOM elements that you want to manipulate, and let main thread do the job.

Is XMLHttpRequest still used?

In the initial stages, XMLHttpRequest used to fetch XML data over HTTP hence the name. But today it can be used with protocols other than HTTP and it can fetch data not only in the form of XML but also JSON , HTML or plain text.

Can service workers access IndexedDB?

Note: IndexedDB can be used inside a service worker for data storage if you require it.

Why fetch is better than XMLHttpRequest?

The Fetch API allows you to make network requests similar to XMLHttpRequest (XHR). The main difference is that the Fetch API uses Promises, which enables a simpler and cleaner API, avoiding callback hell and having to remember the complex API of XMLHttpRequest.


2 Answers

Instead of XMLHttpRequest, you can use the new Fetch API. XMLHttpRequest has been deprecated and is not available in the service worker scope.

There's an example of exactly what you want to achieve in this ServiceWorker Cookbook recipe.

like image 57
Marco Castelluccio Avatar answered Oct 06 '22 13:10

Marco Castelluccio


You can use fetch() instead of XHR,as fetch() is a new API which allows you to make requests similar to XHR's, but has a simpler / friendly API.Read more about fetch here. Try this:-

self.addEventListener('push', function(event) { 
    console.log('Push message', event);
  event.waitUntil(
fetch('/myfileWithJson.php').then(function(response){
return response.json();
}).then(function(data){console.log(data);
    return self.registration.showNotification(data.title, {
      body: data.body,
      icon: 'images/icon.png',   
      tag: data.url
    
    });
}));
});
like image 32
R C Avatar answered Oct 06 '22 14:10

R C