Service Worker — More Power to Web

Progressive Web Applications
In web development, Progressive Web Applications is the new technology shift that everyone’s talking about. So, what exactly is a PWA?
“A progressive web app uses modern web capabilities to deliver an app-like user experience.”
Modern web capabilities.? A lot of powerful APIs have been developed to provide better hardware access and control to web apps. whatwebcando.today tells you what your web browser can do. PWAs are progressive, implying that the app performs better overtime by understanding the user behaviour while using the application. Case studies of Flipkart, Twitter and Alibaba suggest that users are preferring PWAs as they do not require installation on the user’s device but packs the functionality of a native application. Service Worker API is one such powerful javascript API that provides access to traditionally native features like Push Notifications, Offline Mode, etc.
What is a Service Worker?
Service Worker is an event driven Javascript thread, that runs in the background, independent of the web app or other threads. It also acts as a proxy server between the web app, browser and the network (when available). A service worker can be active even when the web app is closed which makes it capable of pushing notifications, background sync, etc.
What can a SW do?
SW API adds conventionally native features like push notifications, geofencing, etc to web applications bringing them closer to native-like experiences. The programmable cache control and the request intercepting capability of a service worker can be coupled to optimise offline experience of a web application. Each feature that SW brings to web is explained below.
Web Push Notifications:
Currently, ZoomRx makes use of one of the features of Service Worker API, web push notifications.
How does web push notifications work?
A web application has to register it’s SW with the browser and request permission to send notifications. Once allowed, the browser has an endpoint to which we can push notifications. Even if the web app is closed when a message is pushed, the ‘push’ event on SW is triggered and it displays the message to the user.
Cache Control
The cache control gives the developer complete control over when and what something goes in and out of the cache. The SW uses it’s own Cache API which cannot be easily overwritten as the browser cache. Cache API of SW even allows to store requests and responses as key-value pairs so that, when a SW intercepts a request from the web app, it can return matching response from the cache instead of contacting the server. Jake Archibald’s offline-cookbook provides some great patterns and methods that can be used while caching. Even with intermittent/no network connection, the user experience of the web application can be enhanced using caching effectively.
Background Sync
Background sync is an API that is built on top of SW API. When a user’s browser has intermittent or no network connectivity, sync allows the web app to fire synchronise events to contact the server at defined intervals or on changes in the state of connectivity. Usually, an exponential backoff is employed to ensure a healthy utilisation of client resources.
Task Scheduling is another API built on top of SW API. Tasks can be scheduled using this API and these tasks will be handled by SW in the background, even if the user closes the web application. For example, when a user records a voice response, submits it and wants to navigate away or to the next question, instead of saying “I want you to stare at the spinner for some more time”, a task can be created and the sync can happen in the background(at the same time or later, depending on the network connectivity).
Geofencing
Geofencing is the use of GPS or RFID technology to create a virtual geographic boundary, enabling software to trigger a response when a mobile device enters or leaves a particular area.
The ability to trigger events based on the location of a device was limited to native applications before SW. The implementation of Geofencing API in browsers was introduced in Chrome for a brief period and then abandoned.
The other side of the coin…
With great power comes great responsibility — Ben Parker
A service worker has the ability to intercept and modify requests, as well as responses from and to a web application. If the origin is not secure, these can be prone to man-in-the-middle attacks. So, Service Worker API can only be used on HTTPS, except for localhost which is treated as a secure origin.
SW is a recent release for the web, so the browser support for most of the features of SW are limited to the latest browsers. The support for SW is under development for Edge. IE does not and will not support SW. SW development is still under consideration for Safari on iOS.

