Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service Worker vs Shared Worker

What is the difference between Service Worker and Shared Worker?

When should I use Service Worker instead of Shared Worker and vice versa?

like image 396
Lewis Avatar asked Mar 05 '15 16:03

Lewis


People also ask

What is a shared worker?

The SharedWorker interface represents a specific kind of worker that can be accessed from several browsing contexts, such as several windows, iframes or even workers. They implement an interface different than dedicated workers and have a different global scope, SharedWorkerGlobalScope .

What is the difference between a service worker and a web worker?

Unlike web workers, service workers allow you to intercept network requests (via the fetch event) and to listen for Push API events in the background (via the push event). A page can spawn multiple web workers, but a single service worker controls all the active tabs under the scope it was registered with.

What is a service worker?

What is Service Worker: A service worker is a script that runs independently in the browser background. On the user side, it can intercept its network requests and decide what to load (fetch).

What is the role of service worker?

A service worker is responsible for assisting the community welfare development by providing social services to an organization or specific individual groups, supporting their needs, and addressing their community concerns.


2 Answers

A service worker has additional functionality beyond what's available in shared workers, and once registered, they persist outside the lifespan of a given web page.

Service workers can respond to message events, like shared workers, but they also have access to additional events. Handling fetch events allows service workers to intercept any network traffic (originating from a controlled page) and take specific actions, including serving responses from a Request/Response cache. There are also plans to expose a push event to service workers, allowing web apps to receive push messages in the "background".

The other major difference relates to persistence. Once a service worker is registered for a specific origin and scope, it stays registered indefinitely. (A service worker will automatically be updated if the underlying script changes, and it can be either manually or programmatically removed, but that's the exception.) Because a service worker is persistent, and has a life independent of the pages active in a web browser, it opens the door for things like using them to power the aforementioned push messaging—a service worker can be "woken up" and process a push event as long as the browser is running, regardless of which pages are active. Future web platform features are likely to take advantage of this persistence as well.

There are other, technical differences, but from a higher-level view, those are what stand out.

like image 182
Jeff Posnick Avatar answered Oct 04 '22 15:10

Jeff Posnick


A SharedWorker context is a stateful session and is designed to multiplex web pages into a single app via asynchronous messaging (client/server paradigm). Its life cycle is domain based, rather than single page based like DedicatedWorker (two-tier paradigm).

A ServiceWorker context is designed to be stateless. It actually is not a persistent session at all - it is the inversion of control (IoC) or event-based persistence service paradigm. It serves events, not sessions.

One purpose is to serve concurrent secure asynchronous events for long running queries (LRQs) to databases and other persistence services (ie clouds). Exactly what a thread pool does in other languages.

For example if your web app executes many concurrent secure LRQs to various cloud services to populate itself, ServiceWorkers are what you want. You can execute dozens of secure LRQs instantly, without blocking the user experience. SharedWorkers and DedicatedWorkers are not easily capable of handling many concurrent secure LRQs. Also, some browsers do not support SharedWorkers.

Perhaps they should have called ServiceWorkers instead: CloudWorkers for clarity, but not all services are clouds.

Hopefully this explanation should lead you to thinking about how the various Worker types were designed to work together. Each has its own specialization, but the common goal is to reduce DOM latency and improve user experience in web based applications.

Throw in some WebSockets for streaming and WebGL for graphics and you can build some smoking hot web apps that perform like multiplayer console games.

like image 29
Dominic Cerisano Avatar answered Oct 04 '22 15:10

Dominic Cerisano