npm

MessageBus
TypeScript icon, indicating that this package has built-in type declarations

1.2.2 • Public • Published

MessageBus Build Status

PubSub library for WebWorkers

Install

$ npm install
$ gulp dist

Usage Example

main.js

var worker = new Worker('worker.js'),
    bus    = MessageBus.create(worker);
 
bus.on('pong', function (payload) {
  console.log(payload.foo);
});
 
bus.emit('ping');

worker.js

importScripts('MessageBus.js');
 
var bus = MessageBus.create(self);
 
bus.on('ping', function () {
  bus.emit('pong', { foo: 'Hello World' });
});

SharedWorker Example

events.ts

enum Events { PONG, PING }

main.ts

var worker = new SharedWorker('worker.js'),
    bus    = MessageBus.create(worker.port);
 
worker.port.start();
 
bus.on(Events.PONG, function (payload) {
  console.log(payload.foo);
});
 
bus.emit(Events.PING);

worker.ts

importScripts('MessageBus.js', 'events.js');
 
onconnect = function (event) {
  var port = events.port[0],
      bus = MessageBus.create(port);
 
  port.start();
 
  bus.on(Events.PING, function () {
    bus.emit(Events.PONG, { foo: 'Hello World' });
  });
};

Readme

Keywords

none

Package Sidebar

Install

npm i MessageBus

Weekly Downloads

0

Version

1.2.2

License

MIT

Last publish

Collaborators

  • icholy