xev

Global event emitter

  • xev

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
xev
3.0.24 months ago5 years agoMinified + gzip package size for xev in KB

Readme

xev
!npm-badgenpm-link !travis-badgetravis-link !mit-badgemit
Global event system for Node. It works well on the cluster! (Messaging between master and workers)

Install

$ npm install xev --save

Usage

It is the same as using EventEmitter. But all events are shared globally.

Simple usage

File A:
import Xev from 'xev';

const ev = new Xev();

ev.on('my-event', message => {
	console.log(`message received: ${message}`);
});

File B:
import Xev from 'xev';

const ev = new Xev();

ev.emit('my-event', 'yo'); // <= 'message received: yo'

On the cluster

If you use the cluster, You must be call mount function at the master process. e.g.:
import { isMaster } from 'cluster';
import Xev from 'xev';

const ev = new Xev();

if (isMaster) {
	// your master code

	ev.mount(); // Init xev
} else {
	// your worker code
}

Worker A:
import Xev from 'xev';

const ev = new Xev();

ev.on('my-event', message => {
	console.log(`message received: ${message}`);
});

Worker B:
import Xev from 'xev';

const ev = new Xev();

ev.emit('my-event', 'yo'); // <= 'message received: yo'

Technically, Node.js cannot workers to communicate directly with each other - all communication goes via the master. So, you must be call our mount initialize function.

That is it.

Good luck, have fun.

API

Please see EventEmitter. In the following, we will describe the unique API of xev.

new Xev(namespace?)

If you are a library developer, we recommend setting namespace to avoid conflicts with events of users or other libraries:
import Xev from 'xev';

const ev = new Xev('my-namespace');

xev.mount()

If you want to share events on the cluster, please call this method once in the master process.

License

MIT