Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does service worker runs on background even if browser is closed?

I see push notification for Facebook web in chrome only when I open chrome. I know that this notification are sent through service worker. I am wondering whether this background sync goes on even though browser is closed or only on opening chrome only these service-worker sync process get started and start sending push notification.

like image 355
Jagajit Prusty Avatar asked Aug 29 '16 07:08

Jagajit Prusty


People also ask

Can service workers run background?

Using a service worker, a Progressive Web App (PWA) can do work in the background, even when the user isn't using the app. Service workers used to be reserved for native apps, but they are now also available to PWAs, providing a better offline experience.

Does service worker have access to window?

You cannot use a service worker to control any window object. Service workers run in a worker context (not a browser context); it therefore has no DOM access. Since things like postMessage() is a window function, and window is part of the DOM, you cannot window. postMessage() from a service worker.

Do service workers run on HTTP?

Service workers will register and work just fine as long as you access the localhost domain -- without HTTPS.

What are service workers in browser?

Service workers are specialized JavaScript assets that act as proxies between web browsers and web servers. They aim to improve reliability by providing offline access, as well as boost page performance.


1 Answers

First thing to say is this depends somewhat on the platform. My understanding of chrome is:

On desktop platforms like windows and Mac OS X the browser needs to have some background process running for a service worker to be able to run. On Mac OS X this is quite easy to detect as the browser can have no windows open but the browser still has the glowing dot beneath it.

On mobile platforms it's easier to listen for events and handle them in an efficient manner, so in these cases the platform can wake up the browser which will then handle any corresponding events.

The above applies to any service worker api's.

On Desktop: If the browser is completely closed then service workers can not run and will not dispatch any events (i.e. no push or background sync events)

On Mobile: The events will still be dispatched (i.e. background sync will trigger when the users device comes online and push will be received and cause a push event).

like image 141
Matt Gaunt Avatar answered Oct 05 '22 03:10

Matt Gaunt