I would like to implement communication between webworkers. I read the W3C documentation and I found MessageChannel is one of the ways to do it, but while reading MessageChannel I couldn't understand how to implement communication between workers using messagechannel.
I got this from MSDN
http://msdn.microsoft.com/en-in/library/ie/hh673525(v=vs.85).aspx
Here is also no proper documentation to do it.
I need to know, how can I communicate webworkers using MessageChannel?
Here is the Demo throwing DATA_CLONE_ERR
var worker = new Worker("sub1_worker.js");
worker.onmessage = function(e) {
$("#log").append("<br>" + e.data);
}
var channel = new MessageChannel();
worker.postMessage("ping", [channel.port2]);
channel.port1.onmessage = function(event) {
// Message is in event.data
alert("Message is: " + event.data);
}
channel.port1.postMessage('hello');
$("#send1").click(function() {
var msg = $("#msg").val();
if (msg && msg != "start")
worker.postMessage("ping2");
$("#msg").val("");
})
$("#send2").click(function() {
var msg = $("#msg").val();
if (msg && msg != "start")
worker.postMessage("ping3",[channel.port2]);
$("#msg").val("");
})
and worker
onmessage = getMessage;
function getMessage(e){
if(e.ports[0])
e.ports[0].postMessage("msg from sub worker 1 "+ e.data);
else
postMessage("msg from sub worker 1 "+ e.data);
}
What is a Web Worker? When executing scripts in an HTML page, the page becomes unresponsive until the script is finished. A web worker is a JavaScript that runs in the background, independently of other scripts, without affecting the performance of the page.
RxJS also has a fromWorker that takes the URL to your Web Worker: github.com/Reactive-Extensions/RxJS-DOM/blob/master/doc/…
Web Workers are a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering with the user interface.
The Web Worker API Async functions come with some limitations. As we learned earlier, we can make our code asynchronous by using callbacks, promises, or async/await. These browser and Node APIs really come in handy when we want to schedule and handle long-running operations.
Here a clean example in pure javascript on how to set this up between two workers:
In main thread:
function setup(){
var channel = new MessageChannel();
var worker1 = new Worker("worker1.js");
var worker2 = new Worker("worker2.js");
// Setup the connection: Port 1 is for worker 1
worker1.postMessage({
command : "connect",
},[ channel.port1 ]);
// Setup the connection: Port 2 is for worker 2
worker2.postMessage({
command : "connect",
},[ channel.port2 ]);
worker1.postMessage({
command: "forward",
message: "this message is forwarded to worker 2"
});
}
In worker1.js
:
var worker2port;
var onMessageFromWorker2 = function( event ){
console.log("Worker 1 received a message from worker 2: " + event.data);
//To send something back to worker 2
//worker2port.postMessage("");
};
self.onmessage = function( event ) {
switch( event.data.command )
{
// Setup connection to worker 2
case "connect":
worker2port = event.ports[0];
worker2port.onmessage = onMessageFromWorker2;
break;
// Forward messages to worker 2
case "forward":
// Forward messages to worker 2
worker2port.postMessage( event.data.message );
break;
//handle other messages from main
default:
console.log( event.data );
}
};
In worker2.js
var worker1port;
var onMessageFromWorker1 = function( event ){
console.log("Worker 2 received a message from worker 1: " + event.data);
//To send something back to worker 1
//worker1port.postMessage("");
};
self.onmessage = function( event ) {
switch( event.data.command )
{
// Setup connection to worker 1
case "connect":
worker1port = event.ports[0];
worker1port.onmessage = onMessageFromWorker1;
break;
// Forward messages to worker 1
case "forward":
// Forward messages to worker 1
worker1port.postMessage( event.data.message );
break;
//handle other messages from main
default:
console.log( event.data );
}
};
This shows to how to handle messages from main thread, how you can forward messages from the main thread to the other worker and how to communicate between the workers directly without the main thread being involved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With