Skip to content

TeamBlackCrystal/xev

 
 

Repository files navigation

xev

Global event system for Node. It works well on the cluster! (Messaging between master and workers)

Install

$ npm install @ayuskey/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

About

Node.js Global Event System

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 90.9%
  • JavaScript 9.1%