Skip to content

Web Workers

Eric M. Dantas edited this page Aug 19, 2016 · 1 revision

You can also integrate WebWorker interactions with μBus, all you have to do is:

  // worker.js

  self.onmessage = (info) => {
    self.postMessage(info.data + ' - sup?'); // yo! - sup?
  }
  // main.js

  let worker = new Worker('path/to/my/worker.js');  

  worker.postMessage('yo!');

  worker.onmessage = (info) => {
    // say `bus` is a singleton, and other files have the same instance that's in main.js

    bus.emit('worker-responded', info.data); // yo! - sup?
  }
  // some-other-file.js

  bus.on('worker-responded', (msg) => {
    console.log(`Here's the message from the worker: ${msg}.`)
  });

With that, everybody that's listening on worker-responded will receive the message that came from the worker. Cool, right?

Clone this wiki locally