2020-03-11 13:14:10 -04:00
# Background processing using web workers
2019-04-01 10:43:20 -04:00
2020-03-11 13:14:10 -04:00
[Web workers ](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API ) allow you to run CPU-intensive computations in a background thread,
freeing the main thread to update the user interface.
If you find your application performs a lot of computations, such as generating CAD drawings or doing heavy geometrical calculations, using web workers can help increase your application's performance.
2019-04-01 10:43:20 -04:00
2020-03-11 13:14:10 -04:00
< div class = "alert is-helpful" >
2020-03-11 13:14:10 -04:00
The CLI does not support running Angular itself in a web worker.
< / div >
2019-04-01 10:43:20 -04:00
2019-12-02 15:47:19 -05:00
## Adding a web worker
2019-04-01 10:43:20 -04:00
2020-03-11 13:14:10 -04:00
To add a web worker to an existing project, use the Angular CLI `ng generate` command.
`ng generate web-worker` *location*
You can add a web worker anywhere in your application.
For example, to add a web worker to the root component, `src/app/app.component.ts` , run the following command.
2020-04-02 12:47:02 -04:00
`ng generate web-worker app`
2020-03-11 13:14:10 -04:00
The command performs the following actions.
2019-04-01 10:43:20 -04:00
2020-03-11 13:14:10 -04:00
- Configures your project to use web workers, if it isn't already.
- Adds the following scaffold code to `src/app/app.worker.ts` to receive messages.
2019-04-01 10:43:20 -04:00
2019-09-09 15:53:37 -04:00
< code-example language = "typescript" header = "src/app/app.worker.ts" >
2019-04-01 10:43:20 -04:00
addEventListener('message', ({ data }) => {
const response = `worker response to ${data}` ;
postMessage(response);
});
2019-09-09 15:53:37 -04:00
< / code-example >
2019-04-01 10:43:20 -04:00
2020-03-11 13:14:10 -04:00
- Adds the following scaffold code to `src/app/app.component.ts` to use the worker.
2019-04-01 10:43:20 -04:00
2019-09-09 15:53:37 -04:00
< code-example language = "typescript" header = "src/app/app.component.ts" >
2019-04-01 10:43:20 -04:00
if (typeof Worker !== 'undefined') {
// Create a new
const worker = new Worker('./app.worker', { type: 'module' });
worker.onmessage = ({ data }) => {
2019-06-26 09:04:06 -04:00
console.log(`page got message: ${data}`);
2019-04-01 10:43:20 -04:00
};
worker.postMessage('hello');
} else {
2019-12-02 15:47:19 -05:00
// Web workers are not supported in this environment.
2019-04-01 10:43:20 -04:00
// You should add a fallback so that your program still executes correctly.
}
2019-09-09 15:53:37 -04:00
< / code-example >
2019-04-01 10:43:20 -04:00
2020-03-11 13:14:10 -04:00
After you generate this initial scaffold, you must refactor your code to use the web worker by sending messages to and from the worker.
2019-04-01 10:43:20 -04:00
2020-03-11 13:14:10 -04:00
< div class = "alert is-important" >
2019-04-01 10:43:20 -04:00
2020-03-11 13:14:10 -04:00
Some environments or platforms, such as `@angular/platform-server` used in [Server-side Rendering ](guide/universal ), don't support web workers. To ensure that your application will work in these environments, you must provide a fallback mechanism to perform the computations that the worker would otherwise perform.
2019-04-01 10:43:20 -04:00
2020-03-11 13:14:10 -04:00
< / div >