web workers in angular

Published: Sun Jul 31 2016

One really cool thing about Angular is that the framework is decoupled from the DOM via a higher level API. In the following post I will show how to take advantage of this by running an Angular application in a web worker.

Configuring an Angular application to run as a web worker requires a little bit more setup code than a regular browser application. In this article I will go through a simple example that you can build on to make more complex applications later.

I will start by creating a simple component. In my sample I have created a component for calculating n-factorial. The idea is that a value n will be passed to the component from the outside. Then, the component will calculate n-factorial based on n and return the result.

If we look at the component code in isolation it's not really that different from regular components.

The main difference is the platformWorkerAppDynamic().bootstrapModule(WorkerModule) call that allows us to bootstrap the application in a web worker.

There is also a ServiceMessageBrokerFactory at play here to facilitate web worker communication. Web worker communication is message based, so whenever we receive a message over the correct channel, the calculate method will be called. The calculate method is registered with the broker based on meta data with information about its input and output types. The PRIMITIVE symbol means the method requires primitives types like string, number and boolean as input and output.

The message broker expects the calculate method to return a promise

Next I have created a loader (loader.ts) to load the component we just created.

In main.ts I kick of the loading of the web worker, but I also define a channel for communication between the host application and the web worker.

Once the worker application is bootstrapped I create a message broker to create a two way channel between the host and the web worker. Through this channel I can pass in the value 'n' and get the calculated factorial value in return.

The setup in the post bootstrap method matches the message broker definitions in the web worker component.

This was just a taste of what's possible with web workers in Angular 2.0, but I have added a demo here if you are interested.