2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								# Background processing using web workers
 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07: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 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								< div  class = "alert is-helpful" > 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								The CLI does not support running Angular itself in a web worker.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< / div > 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-12-02 12:47:19 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								## Adding a web worker
 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								To add a web worker to an existing project, use the Angular CLI `ng generate`  command.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-08-12 10:44:45 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								```bash
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								ng generate web-worker < location > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								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-08-12 10:44:45 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								```bash
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								ng generate web-worker app
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2020-04-02 19:47:02 +03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								The command performs the following actions.
							 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07: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 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-09 12:53:37 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  < code-example  language = "typescript"  header = "src/app/app.worker.ts" > 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								  addEventListener('message', ({ data }) => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    const response = `worker response to ${data}` ;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    postMessage(response);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  });
							 
						 
					
						
							
								
									
										
										
										
											2019-09-09 12:53:37 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								 < / code-example > 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								-  Adds the following scaffold code to `src/app/app.component.ts`  to use the worker.
							 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-09 12:53:37 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  < code-example  language = "typescript"  header = "src/app/app.component.ts" > 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								  if (typeof Worker !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    // Create a new
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    const worker = new Worker('./app.worker', { type: 'module' });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    worker.onmessage = ({ data }) => {
							 
						 
					
						
							
								
									
										
										
										
											2019-06-26 15:04:06 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								      console.log(`page got message: ${data}` );
							 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								    };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    worker.postMessage('hello');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  } else {
							 
						 
					
						
							
								
									
										
										
										
											2019-12-02 12:47:19 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    // Web workers are not supported in this environment.
							 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								    // You should add a fallback so that your program still executes correctly.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2019-09-09 12:53:37 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  < / code-example > 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07: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 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								< div  class = "alert is-important" > 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07: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 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								< / div >