Is it possible to implement a web worker with GWT?

299 views
Skip to first unread message

Bruno Salmon

unread,
Oct 1, 2020, 4:43:37 PM10/1/20
to GWT Users
hi,

I have a GWT web app that I would like to make more reactive by moving some heavy computation code into web workers. This code doesn't need the context of the application to run, I just need to pass it a few input parameters so it should be a good fit for a web worker.

I see how the web app can instantiate web workers using elemental2.dom.Worker.

But as my worker code is in Java, I would like to know if I can use the GWT compiler to generate the worker itself. I expect a js file with a onmessage function as output that I can load from the web app.

Is it possible to do this?

Thomas Broyer

unread,
Oct 2, 2020, 11:04:55 AM10/2/20
to GWT Users
It is, through a linker: http://www.gwtproject.org/doc/latest/DevGuideLinkers.html
It might work with the D8ScriptLinker shipped in GWT (https://github.com/gwtproject/gwt/blob/2.9.0/dev/core/src/com/google/gwt/core/linker/D8ScriptLinker.java); you'll have to declare it in a *.gwt.xml though (define-linker then add-linker).
Or there might be (IIRC, there are) libraries with such a linker specifically for workers.

lofid...@gmail.com

unread,
Oct 2, 2020, 12:14:03 PM10/2/20
to GWT Users
I'm not sure, will this project helps you: https://gitlab.com/ManfredTremmel/gwt-webworker?

Bruno Salmon

unread,
Oct 5, 2020, 6:17:32 AM10/5/20
to GWT Users
Thank you both for your inputs.

Yes the gwt-worker is the linker I was looking for :)

However the produced JS file breaks because it still contains some references to the window object which doesn't exist in the context of a web worker.

Is there an option in the GWT compiler to tell that the context is a web worker and that it should not use such objects but "self" instead?

Otherwise I noticed the linker already produces some patch code before calling GWT, the first JS line looks like this:
$stats=function(){};$self=self;$wnd=self;$doc=self;$sessionId=null; etc...

Manually adding window=self; in that line actually solves the JS break issue.

In the probable case GWT has not been designed for web workers and we can't prevent it producing code without refering to window, I will open an issue in the gwt-worker linker repository to ask adding window=self; in the patch line. 

ji...@jinq.org

unread,
Oct 16, 2020, 6:52:52 PM10/16/20
to GWT Users
I use a GWT web worker in one of my apps. I ended up hacking the existing cross-site linker to leave out the stuff that make things messy (such as super-dev mode).

I'm not sure everything it there, but you should be able to just dump everything in the package com.wobastic.gwt.webworker.linker and go:

Shawn Brown

unread,
Nov 5, 2020, 6:04:59 AM11/5/20
to google-web-toolkit
Hi,

I'm not sure, will this project helps you: https://gitlab.com/ManfredTremmel/gwt-webworker?

I really want to use it, but can't figure out the docs.

In the GWT side, it says to use:

Worker worker = Worker.create(<javascriptname>) 
...
worker.postMessage("(may be serialized) data to handle in web worker"); 

What is the javascriptname?

If MyWebWorker.gwt.xml renames my webwork module to "mywebworker.wkr", do I use something like:

Worker.create("/mywebworker.wkr/mywebworker.wkr.nocache.js");

Other examples online seem to suggest so, but mywebworker.wkr.nocache.js isn't being generated in compilation so am stuck on what to do.

Anyone?

Shawn

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit/0498c25c-1bd1-471c-aaa7-c9bba70488ecn%40googlegroups.com.

Shawn Brown

unread,
Nov 5, 2020, 6:43:47 AM11/5/20
to google-web-toolkit
Reply all
Reply to author
Forward
0 new messages