2

If I open a web app using a web browser or a PWA by clicking the icon on my home screen, can that app still send HTTP requests while it's minimized?

I'm asking this because of say, for example, I want to check out a navigation app to navigate to somewhere but I still want the web app to update the user's location through HTTP requests while it's minimized.

Is it possible or does the app stop "working" while it's not in focus? Is it a service worker implementation needed?

7
  • this totally depends on the browser of the user. Sep 26, 2019 at 6:58
  • Could you elaborate a bit more? Is Chrome needed?
    – jones
    Sep 26, 2019 at 7:03
  • For Chrome, it will continue doing work for some time before killed by system Sep 26, 2019 at 7:04
  • So basically it's not very reliable, you would have to do native mobile solution in order to achieve what Im trying to do
    – jones
    Sep 26, 2019 at 7:14
  • 1
    Yes, that is right. Sep 26, 2019 at 7:16

1 Answer 1

2

You can achieve your task implementing a PWA. The Service Worker file will run in the background and keep running even when the user closes the app. This is possible since the SW runs on a separate thread.

It is also possible to execute background sync, even though its support is quite limited:

enter image description here

You can let the service worker call the server to send the user's position if internet connection is available, otherwise this information can be stored in a local DB (eg. IndexedDB or Cloud Firestore) and then, once the users gets online again, the changes will be pushed to the server.

The nice thing by using Cloud Firestore is that the synchronisation is made out of the box for you. I wrote an article about the usage of Firestore with PWAs to overcome the limits of CACHE API. It is within a series of articles about PWAs, have a look st tit if you want to deepen the topic.

7
  • The other gentleman said it was not possible so now im a little confused.
    – jones
    Sep 26, 2019 at 11:35
  • As you can see from the scheme above, background sync depends from the browser. So if you are using Firefox it will not work. But if you can rely only on chrome or opera, for example, then it is possible. Maybe he meant it is not possible to provide a general solution that works across multiple/all browsers.
    – Francesco
    Sep 26, 2019 at 11:57
  • It seems like the background sync is a one-off function call, I will need to update somebody's geolocation every 10 seconds while the app is open in the background. Therefore it looks like I need a native solution
    – jones
    Sep 26, 2019 at 12:17
  • 1
    I could imagine a solution that given a specific interval (10 sec for you) checks if internet is available, if so it will call the endpoint to register the position, otherwise it will save it in the local DB. Once the user has connection again, the SW will access the DB and executes one by one the updates to the server. Actually with a bit of custom logic you can achieve this without background sync. But of course it is up to you to implement the best solution fitting your requirements.
    – Francesco
    Sep 26, 2019 at 12:40
  • @Francesco: Do you really mean when the user closes the app, or just minimizes it? I don't think it's possible if the app is not running at all. Jun 29, 2020 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.