I have a page where I want to use a set of WebWorkers to perform tasks in the background. Each WebWorker has a specific function, but potentially useful to others. It is simple to receive / send messages to and from a WebWorker created by the page (or created by a specific WebWorker in the case of sub-workers), since there is a reference to Worker
if you add listeners or call methods:
var worker1 = new Worker("script1.js");
worker1.postMessage("bla");
worker1.onmessage = function(mensagem) {
alert(mensagem.data);
};
var worker2 = new Worker("script2.js");
...
But if, say, script1.js
wants to send a message to worker2
, is it possible to do so without "routing" the message through the main thread ? (i.e. onmessage
of worker1
receives the message and makes a postMessage
pro worker2
)
The reason is performance: such as HTML5 Web Messaging serializes the message parameters (and at the time of writing most browsers still do not support% s of% s), this "routing" would require that the data be serialized and de-serialized twice, even though the overhead of if sending two messages was negligible (which is not always true, since certain applications - WebGL for example - can keep the main thread busy for a long time).