Transparent multi instance EventEmitter using Redis
MIT License
.on() and .emit() EventEmitter API
npm install --save merkury
mk.on("my-event",..);
mk.emit("my-event",..);
on one of your instances and every const Merkury = require("merkury");
const ioRedisConfig = {
host: "localhost",
port: 6379
};
const mk = new Merkury("unique-service-name", ioRedisConfig, true);
//super easy API just like the usual event-emitter:
mk.on("my-event", (arg1, arg2, arg3) => { .. });
mk.emit("my-event", "arg1", {}, "arg3");
//some advanced sugar available:
mk.disconnect().then(_ => { .. });
mk.setConfig() / mk.setTopic();
mk.connect().then(_ => { .. }); //reconnect with topic switch
mk.pause();
mk.resume() //pause & resume handler
//subscribe to error events
mk.on("error", err => { .. });
mk1.on("my-event", arg1 => { console.write(arg1) });
mk2.on("my-event", arg1 => { console.write(arg1) });
mk3.on("my-event", arg1 => { console.write(arg1) });
mk1.on("my-race-event", arg1 => { console.write(arg1) }, true);
mk2.on("my-race-event", arg1 => { console.write(arg1) }, true);
mk3.on("my-race-event", arg1 => { console.write(arg1) }, true);
//with race mode disabled (usually)
mk1.emit("my-event", 1); //output: 1\n1\n1\n
//with race mode enabled
mk1.emit("my-race-event", 1, 2, 3); //output: 1\n
//race mode uses "redlock" algorithm to ensure only a single
//Merkury{} instance will call its EventListener
//merkury takes track of its race enabled events and is able
//to remove them permanently when using e.g. mk1.removeListener(..)