Is it possible to send messages from one WebWorker to another WebWorker?

7

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).

    
asked by anonymous 11.12.2013 / 23:28

1 answer

4

You can try using MessageChannel to communicate between the two WebWokers . I'm working on a sample code in JsFiddle , but you can see something functional in these links:

12.12.2013 / 18:34