Global event system for Node. It works well on the cluster! (Messaging between master and workers)
$ npm install @ayuskey/xev --save
It is the same as using EventEmitter. But all events are shared globally.
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'
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.
Good luck, have fun.
Please see EventEmitter. In the following, we will describe the unique API of xev.
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');
If you want to share events on the cluster, please call this method once in the master process.