Matt Brubeck

22 Apr 2010

Headless Web Workers: Does the web need background apps?

At my last job, I created several web applications designed to replace built-in apps on mobile phones. While modern browsers and HTML5 made this incredibly easy in many ways, we still ended up writing native (i.e. non-web) code for most of our applications. There were a few different areas where the browser alone didn’t meet our needs, but one that I found suprisingly common was background processing.

Consider the following mobile applications:

Ideally, each of these apps will perform some actions even when the user does not have it open. (Background processing is not strictly necessary for the e-reader, but it would be useful to ensure the library is up-to-date even when opened in a place with no network connection.)

You can’t do this with a web app. Web Workers don’t solve the problem, because they run only while the web page is open. What we need are headless web workers.1

Update (2010-04-26): Gordon Anderson points out in comments that members of Google’s Chromium/Chrome OS projects have made a very similar proposal they call Persistent SharedWorkers.

The API

Headless workers could use almost the same API as Web Workers. Instead of responding to messages from a web page, they would listen to events from the host system (browser or OS). These events might include time intervals, power-on/resume, changes in network connection, geographic locations, or “push” notifications from a remote server.

The event-driven architecture of JavaScript in the browser allows the host system a high level of discretion over resource consumption. There’s no special code needed to suspend processes and later restore their state, because JavaScript workers are naturally inactive between events. The host can provide limits on CPU or memory usage per event, with a separate message to notify processes whose handlers were aborted. And it can limit the number of concurrent processes by choosing when to dispatch events to listeners. Some listeners could even be disabled completely at times (like if the device is busy or the battery is low), and notified later of the events they missed.

This is almost a return to the old days of cooperative multitasking. Mobile computing is definitely driving everyone towards higher-level process control in the OS, and different assumptions for applications. It’s not surprising that my whole proposal resembles Android and iPhone 4.0 multitasking in several ways, since I’ve been doing development on Android for the last 18 months and encountering many of the same issues.

The UI

Headless workers do need some way to interact with the user. They could display standard system notifications (via Growl on the Mac, libnotify on Ubuntu, the status bar in Android, etc.) using W3C Web Notifications, which already have an experimental implementation in Chrome.

Users also needs to know which sites have background tasks installed. Headless workers could be represented by icons in a standard location (perhaps a toolbar in desktop browsers, or the home screen on a mobile device). The icons could display ambient status; clicking one would reveal a menu with options to configure or remove it.

Questions

This proposal might be hard to standardize, especially where it’s tied to specific OS capabilities. For now I’m just curious: would it be useful? You can write a native app or a browser extension to solve this problem today. But would it be worthwhile to have a standard, cross-platform way to do it? Has anyone else run into problems that this approach could solve?

  1. Because all web standards should have names that sound like Harry Potter creatures.