How to use web workers in dart

402 views
Skip to first unread message

regard...@gmail.com

unread,
Jan 20, 2016, 11:40:21 AM1/20/16
to Dart Web Development
I was wondering if there is a way to use web workers in such a way as to have the following:
  • write the code as a whole cohesive app
  • run and debug main app and worker in dartium
  • compile to js and run main app and worker without manual changes
  • use all worker apis available in modern browsers
It would be really great if anyone can link to a small example project for this or a link to a blog post or similar explaining how to use those technologies in an easy manner.

Thanks

Kasper Peulen

unread,
Jan 20, 2016, 11:48:26 AM1/20/16
to w...@dartlang.org
I'm very interested as well.

--
You received this message because you are subscribed to the Google Groups "Dart Web Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web+uns...@dartlang.org.
Visit this group at https://groups.google.com/a/dartlang.org/group/web/.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/web/ed0e787c-8f96-42a8-8a66-35d734f2e41b%40dartlang.org.



--
Kasper

Nathan Collins

unread,
Jan 20, 2016, 1:02:15 PM1/20/16
to w...@dartlang.org

Yegor Jbanov

unread,
Jan 22, 2016, 12:39:17 AM1/22/16
to Dart Web Development, regard...@gmail.com
Writing apps across several processes with separate heaps and only connected via message passing will always have its challenges. In Angular 2 we are trying to address it by moving your entire application to the worker (components, directives, DI). This has several advantages:

- you do not need to break up your app into several processes that communicate via message passing making it one cohesive app (if that's what you meant by "cohesive")
- the worker code can still run on the main thread which makes it easy to debug (you can even run it on a localhost server and debug in WebStorm or debug simultaneously in multiple browsers)

A disadvantage, of course, is that you don't have direct access to the DOM API. For that you still need to do message passing. Alternatively, you can use custom elements (e.g. polymer) that run on the main process (and have DOM access), and have your angular app communicate with them via normal @Input/@Output template bindings.

I admit, I do not fully understand your point about manual changes and js compilation. As for worker APIs, it should be easy to call into it via dart:js, but I'm not sure if dart:js works in Dartium inside isolates. dart2js probably works. Worst case, you can talk to the UI process via messages and have a piece of code there access browser API. Curious, which web worker API would you like access to that you do not have today?

Peter StJ

unread,
Jan 22, 2016, 6:45:27 AM1/22/16
to Yegor Jbanov, Dart Web Development
Thank you for your answer.

My question was more general. The scenario is simple and straight forward.
I have a web app that on some user action is performing some calculations and when ready displays the result. Thus far we can assume that:
  • I have the UI/DOM logic completely separated from the compute intensive part, actually it is in another library
  • UI and calculating part communicate indirectly (using some facade or proxy pattern, the main thing is that it returns promises to UI)

The question here is this:
what are the steps (more specifically which one is the dart way) to make the computation code run in web worker when compiled to JS and how is this working while in dartium?

One approach that was advised in 2014 was to have two separate projects and compile both to js and use worker class in main app which was not very clear how does this deal with dartium as I had to use js files even in development, another one was to use spawnUrl which is even more confusing.

I am doing this in JS with Worker class and indeed the main app file and the worker's main file are separate entry points and are compiled independently (closure compiler).

What I am interested in is what is the was to have this functionality in dart in such a way as to allow me to write only dart and test only dart (main app and worker app - if separate entry points instead of one app and run internally as worker by something like zone or isolate), but then get compiled version that still works as JS for deployment and does not require changes - for example to include something special in html or change URL (maybe I need to include the worker file somehow if it is generated etc).   

Possibly I am looking for the simplest example how to do the web worker intro as found on MDN but in dart (https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers)

Thanks

----
Петър
Reply all
Reply to author
Forward
0 new messages