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);
}