'Fetch data from firebase/firestore with service worker
I built a PWA that fetches data from firestore. I want to run some checks on that data every couple of hours which is why I want to use a service worker for that.
This is a snippet from my working code:
billsCollection.orderBy('dayOfMonth', 'asc').onSnapshot(querySnapshot => {
let billsArray = [];
querySnapshot.forEach(doc => {
let bill = doc.data();
bill.id = doc.id;
billsArray.push(bill);
});
store.commit('setBills', billsArray);
});
If I try to run .onSnapshot in my service worker I get the message that xmlhttprequest is not known or similar.
It seems like 'firebase/app' and 'firebase/firestore' use the old xmlhttprequest to communicate with firebase/firestore. Service Workers only support the newer fetch api. Has anybody successfully fetched data from firebase/firestore before using the new fetch api?
I would like to periodically fetch data from firebase using a service worker.
If that does not work, is it possible to check your data with a Cloud Firestore function? I am not sure since it would need a time event trigger or similar.
Thanks everybody for your ideas, Johannes
Edit:
As Scarygami pointed out I can solve that problem by using a cron service that triggers a firestore function. This works. Nevertheless, I am still looking for a way to fetch data from firestore with a service worker directly. Then I would not need a 3rd party cron service or cloud functions and the specific token of the device I want to send a message to. It would simply fetch data, check data, show notification if needed.
Solution 1:[1]
It might not have been possible at the time the question was asked but meanwhile it is possible.
See my other answer here: https://stackoverflow.com/a/71014462/3243751
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | FLUXparticle |
