Fork me on GitHub

worky

An EventEmitter like interface for web-workers.

Download .zip Download .tar.gz View on GitHub

Demo

// The main thread (main.js)
(function () {
    var w = new Worky("js/worker.js")
      , workerObj = new Worker("js/worker2.js")
      , w2 = new Worky(workerObj)
      ;

    // Listen for events from worker
    w.on("some event", function (data) {
        console.log("worker->window received (some event):", data);
    }).on("hello world", function (data) {
        console.log("worker->window received (hello world):", data);
    });

    // Emit something to the worker
    console.log("window->worker (another event)");
    w.emit("another event", {
        "hello": "world"
    });

    // Listen for events from worker2
    w2.on("some event", function (two, numbers) {
        console.log("worker2->window received (some event):", two, numbers);
    });

    // Emit something to the second worker
    console.log("window->worker2 (hello to worker2)");
    w2.emit("hello to worker2", { magic: 42 });
})();

// worker.js
// import worky
importScripts("../src/worky.js");

// Create the event interface
var worker = Worky();

// Emit something back to the window
worker.emit("hello world", { "yes!": "it works!" });

// Listen for events from window
worker.on("another event", function (data) {
    worker.emit("some event", data);
});

// worker2.js
// import worky
importScripts("../src/worky.js");

// Create the event interface
var worker = Worky();

// Listen for events from window
worker.on("hello to worker2", function (data) {
    worker.emit("some event", data.magic, 7);
});

                    
Check out the documentation on GitHub.