Worker.IO

Messaging pub/sub api for web workers includes Parallel libary


Keywords
pool, workers, task, parallel, threads, pub/sub, wroker, webWorkers
License
MIT
Install
bower install Worker.IO

Documentation

WorkerIO


Messaging pub/sub api for web workers includes Parallel libary

NPM

New: Parallel functionality - with an ability to send job and split it to sub workers More details below

The new parallel libary including those features:

  • pub/sub - send and get messages from the job during proccessing
  • waitAny - get a notification on the caller side when a sub worker finish its job with the result of the job.
  • WaitAll - get a notification on the caller side when a the whole job is finshed includes the result of the job
  • broadcasting - broadcasting message to the whole wrokers
  • cancelAll - close the workers
  • limiting worker numbers- (in the next few days) an ability to limit the number of workers that will handle the job

More details below under the user instructions section

Usage instructions:

worker:

  • create a worker on a separated file

  • import workerIO lib to your worker using importScripts() then

  • init workerIO var yourWorkerName = IO.Worker();

  • call to start method you should recive your socket instance for communicating with the caller side

    yourWorkerName.start(function(socket){  
                            \\body   ....    });
  • within the start function you can recive messages by calling to on function
    socket.on("messageName",callbackFunction(Data))
  • sending messages using emit function socket.emit("messageName","Data")
  • full worker sample code
\\"testWorker.js" file
function Worker() {
  importScripts('../dist/WorkerIO.js');
  var testWorker = IO.Worker();
  testWorker.start(function(socket){
    socket.on("emitTest",function(data){
      console.log(data);

    })
    socket.emit("test","bla")
  })
  Worker();

caller:

  • init workerIO caller var yourCallerName = IO.Reciver("testWorker.js");
  • start processing the job by calling to start function
    yourCallerName.start(function(socket){
                          ...      })
  • within the start function you can recive messages by calling to on function javascript socket.on("messageName",callbackFunction(Data))

  • sending messages using emit function javascript socket.emit("messageName","Data")

  • full caller sample code

 var io= IO.Reciver("testWorker.js");
  io.start(function(socket){
    socket.on("test",function(data){
      console.log("on: "+data);
      socket.emit("emitTest","bla");
    });
    socket.emit("emitTest","bla");
  });

parallel:

  • init parallel job is done using var parallel= IO.Parallel("workerUnit.js");
  • in order to start a new job you should use
parallel.foreach([array of jobs...],function(socket,data){
      //communicating with a specific worker
      socket.emit("runParallel",data);
      socket.on("status",function(data){
        console.log("status: "+data);
      })
  • then you can register to the following events
    • on waitAny callback you will get the workerName and the result of the worker
    • on waitAll callback you will get the results from all the workers
parallel.waitAny(function(workerName,data){
    //getting message when each  worker finish its job
    console.log("WaitAny: data was procced on: "+workerName+" with result "+data);
  }).waitAll(function(data){
    //getting message when all the job finsh their job
    console.log("WaitAll: data was procced with result "+data);
  })
  • sending brodacast meesage to all of the workers is done using parallel.broadcast.emit("messageTopic",data)
  • you can also cancel to job during proccessing by using cancelAll parallel.cancelAll();

full parallel example

caller:

  //imitJob
  var parallel= IO.Parallel("workerUnit.js");
  //Setting  Job and creating wrokers
  parallel.foreach([1,2,3,4,5,6,7,8],function(socket,data){
      //communicating with a specific worker
      socket.emit("runParallel",data);
      socket.on("status",function(data){
        console.log("status: "+data);
      })
  }).waitAny(function(workerName,data){
    //getting message when each  worker finish its job
    console.log("WaitAny: data was procced on: "+workerName+" with result "+data);
  }).waitAll(function(data){
    //getting message when all the job finsh their job
    console.log("WaitAll: data was procced with result "+data);
  });
  setTimeout(function(){
    //brodcasting message to the workers
    parallel.broadcast.emit("broadcasting",":)")
  }, 6000);

  function stopJob() {
    //removing all the jobs
    parallel.cancelAll();
  }

worker:

function parallelTest() {
  importScripts('../../dist/WorkerIO.js');
  var inter= null ;
  var testWorker = IO.Worker();
  testWorker.start(function(socket){
    socket.on("runParallel",function(data){
      console.log("runParallel: "+data);
      calc_data(data);
    }).on("broadcasting",function(data){
      console.log("broadcasting: "+data);
      });

    setInterval(function () {
      socket.emit("status","!!!Not finished yet")
    },5000);
    function calc_data(data) {
      inter =  setInterval(function(){
           var calcData = data*5
           socket.returnResult(calcData);
        }, data*1000);

    }
  })
}
parallelTest();