angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

How to work with web workers (use a web worker IN cli project)

Open jbgarr opened this issue 9 years ago • 72 comments

Bug Report or Feature Request (mark with an x)

- [ ] bug report -> please search issues before submitting
- [X] feature request

Versions.

@angular/cli: 1.0.0 node: 6.9.1 os: darwin x64 (macOS Sierra 10.12.3)

Is there a way to import and use a web worker in a component using angular-cli? I'm trying to incorporate a web worker that I wrote into my project, but I'm not having any luck. The web worker worked fine in my project before attempting to convert it to use the CLI (previous version was based on System.js). I'm trying to import my web worker code like this:

import * as MyWorker from 'assets/workers/dropdown.worker.js';

and then I'm trying to create my actual worker instance like this:

let blob = new Blob(['(this.onmessage=', MyWorker.toString(), ')'], { type: "text/javascript" });
let dropdownWorker = new Worker((<any>window).URL.createObjectURL(blob));

The above does not work though, and if I log MyWorker right after I try to import it I see it's just an empty object instead of a function (which is what it is in my other app). Any help on this would be much appreciated!

UPDATE

I just want to clarify what I'm trying to do: I want to use an existing web worker IN my Angular app, not run my whole app IN a web worker. I know there is separate issue already that relates to the trying to run an entire app in a web worker. These are similar, but very different issues.

jbgarr avatar Apr 07 '17 23:04 jbgarr

Thanks @naveedahmed1. May still be of interest to the OP. I'm actually more concerned with #2305, personally. Going to hop out of this thread in favor of that one.

bmayen avatar May 11 '17 15:05 bmayen

@naveedahmed1 Thanks for the plugin link. I'll definitely take a look.

jbgarr avatar May 11 '17 17:05 jbgarr

Let me know if https://github.com/haochi/angular2-web-worker addresses your needs. The importScripts bit looks like it would work pretty well with a file in assets/.

filipesilva avatar May 12 '17 08:05 filipesilva

@filipesilva should the eject based approach work with Lazyloading?

naveedahmed1 avatar May 12 '17 21:05 naveedahmed1

@naveedahmed1 yes, the webpack plugin supports it.

filipesilva avatar May 16 '17 09:05 filipesilva

Here is a proof of concept project for Angular 4 loading web workers: web-worker-in-ng

@jbgarr, I was able to create an Angular 4.x project that implements two web workers, transpiles them, and hashes them in a build. No ng eject. The only "gotcha" I forgot to document in the project is the Web Worker JS file can not be the same name as it's TS file if they reside in the same directory. The webpack worker-loader gets confused and loads the ts file even when explicitely telling it to load the .js file.

jerryorta avatar May 19 '17 20:05 jerryorta

Considering that web workers are designed to offload computation to a background process, it's not clear why running an entire Angular application as a web worker is particularly beneficial. If everything is running in the same process, then what's the point? Another use case for @jbgarr 's original issue here (running a web-worker within an Angular app) is facilitating shared web workers. Among other uses, shared web workers can provide common access to an IndexedDB database for multiple browser tabs and/or iframes.

CalvinDale avatar Sep 10 '17 20:09 CalvinDale

it's not clear why running an entire Angular application as a web worker is particularly beneficial. If everything is running in the same process, then what's the point?

@CalvinDale that's the thing. If you run Angular into a web worker, the "main" thread is kept free. So if your app is huge and the change detection cycles are taking too much time for 60fps rendering (blocking the main thread), having the change detection cycle running in his own thread is a game changer. Because your UI is not freezing all the time and at worst, you'll get a small delay before having a refresh with latest information but no freeze and everything smooth.

maxime1992 avatar Sep 10 '17 21:09 maxime1992

Interesting feedback @maxime1992. I hadn't considered the case of an extremely large site. The sites that I've worked on haven't been large enough to encounter this issue.

CalvinDale avatar Sep 10 '17 23:09 CalvinDale

Also, the way Angular implements this is almost entirely transparent; as long as you're not accessing DOM directly. Huge optimization for free. Managing multiple webworkers manually takes considerably more effort.

bmayen avatar Sep 11 '17 12:09 bmayen

@bmayen thats awesome, do you have any idea how would be handle libraries like hammerjs in case of web workers?

naveedahmed1 avatar Sep 11 '17 12:09 naveedahmed1

The problem is that in order to make this work transparently, you need to use Angular bindings. In that case Angular is smart enough to marshal calls between the worker thread and the DOM behind the scenes to update the DOM for you automatically. Outside of that use-case, you need to handle it yourself.

Not sure about hammerjs specifically, but we had an app which needed to update canvases. Had to write a layer to batch canvas calls into a serializable object, pass it over to the DOM thread, have a method there fetch the canvas context, and run the calls against it.

Similar issue when writing to localstorage, cookies, etc. Basically, anything on the window that you can't update via a template binding.

bmayen avatar Sep 11 '17 13:09 bmayen

I posted a question related to original feature request here on Stack Overflow.

CalvinDale avatar Sep 11 '17 16:09 CalvinDale

Just curious. https://github.com/haochi/angular2-web-worker worked for @jbgarr or anyone else running individual web workers inside angular app?

saurabhpathak9 avatar Feb 05 '18 02:02 saurabhpathak9

@saurabhpathak9 I actually never had a chance to try the plugin, so I'm not sure how it works. Hopefully some others can chime in and provide feedback.

jbgarr avatar Feb 07 '18 01:02 jbgarr

@jbgarr - Thanks for your reply. Somehow, haochi plugin didn't work for me. But I was able to make it work using @jerryorta-dev proof of concept code!

saurabhpathak9 avatar Feb 14 '18 03:02 saurabhpathak9

All - For benefit for all, I created a sample project using ag-grid with high frequency updates on websocket, leveraging web-worker for reconnect to websocket with progressive interval. Hope it helps! Also, thanks again to @jerryorta-dev! Grid with Websocket and Webworker

saurabhpathak9 avatar Feb 28 '18 02:02 saurabhpathak9

I'm sorry for my ignorance if I'm missing something here, but I just read the code of angular2-web-worker and it only wraps a regular web worker in a promise to be consumed. So what's the problem about using regular web workers in the first place? You would only need to add the file in cli config apps[0].assets.web-worker.js won't you?

Yes, I know, you would need to write a Javascript file in an Angular app.

Still, maybe I AM missing something else here, but I don't see angular2-web-worker doing any magic. It is just loading a regular js file at the end, so why not just do that?

michaeljota avatar Mar 16 '18 03:03 michaeljota

@jbgarr - i came with a similar issue where in i have to use webworkers just for some complex functionality and not for the whole angular application in web workers.

I have used web workers as a service in angular 4 application by creating them on the fly using blobs.

You can check the code on https://github.com/rsohi/web_worker_angularCLI

rsohi avatar Apr 17 '18 11:04 rsohi

@rsohi That's one of the things what angular2-web-worker does. So, again, maybe I'm missing something here, but I think a regular .js file for the web worker is acceptable.

michaeljota avatar Apr 17 '18 11:04 michaeljota