In Node.js is there any way to listen to all events emitted by an EventEmitter object?
e.g., can you do something like...
event_emitter.on(\
a monkey patch add onAny method to EventEmitter.
it is useful to be able to monitor only events of one problem.
var EventEmitter=require('events')
var origemit=EventEmitter.prototype.emit;
Object.assign( EventEmitter.prototype, {
emit:function(){
if(this._onAnyListeners){
this._onAnyListeners.forEach((listener)=>listener.apply(this,arguments))
}
return origemit.apply(this,arguments)
},
onAny:function(func){
if(typeof func !== 'function'){
throw new Error('Invalid type');
}
if(!this._onAnyListeners)this._onAnyListeners=[];
this._onAnyListeners.push(func);
},
removeOnAny:function(func){
const index = this._onAnyListeners.indexOf(func);
if(index === -1){
return;
}
this._onAnyListeners.splice(index,1);
}
});
// usage example
//gzip.onAny(function(a){console.log(a)})
Ran into the same problem today, heres a solution:
Object.create(Object.assign({},EventEmitter.prototype, {
_onAnyListeners:[],
emit:function(...args){
//Emit event on every other server
if(this._fireOnAny && typeof this._fireOnAny === 'function'){
this._fireOnAny.apply(this,args)
}
EventEmitter.prototype.emit.apply(this,args)
},
_fireOnAny:function(...args){
this._onAnyListeners.forEach((listener)=>listener.apply(this,args))
},
onAny:function(func){
if(typeof func !== 'function'){
throw new Error('Invalid type');
}
this._onAnyListeners.push(func);
},
removeOnAny:function(func){
const index = this._onAnyListeners.indexOf(func);
if(index === -1){
return;
}
this._onAnyListeners.splice(index,1);
}
}));
As mentioned this behavior is not in node.js core. But you can use hij1nx's EventEmitter2:
https://github.com/hij1nx/EventEmitter2
It won't break any existing code using EventEmitter, but adds support for namespaces and wildcards. For example:
server.on('foo.*', function(value1, value2) {
console.log(this.event, value1, value2);
});
I know this is a bit old, but what the hell, here is another solution you could take.
You can easily monkey-patch the emit function of the emitter you want to catch all events:
function patchEmitter(emitter, websocket) {
var oldEmit = emitter.emit;
emitter.emit = function() {
var emitArgs = arguments;
// serialize arguments in some way.
...
// send them through the websocket received as a parameter
...
oldEmit.apply(emitter, arguments);
}
}
This is pretty simple code and should work on any emitter.
I needed to trace all emitted events in all libraries, so I tapped into the prototype
.
This example uses a Typescript signature
, but you can just remove it if you are not into that kind of nonsense.
Within the call, this
refers to the object that is emitting. It was very easy to track all unique object:emits in my project.
// For my example I use a `set` to track unique emits.
const items = new Set()
const originalEmit = EventEmitter.prototype.emit;
EventEmitter.prototype.emit = function (event: String | Symbol, ...args: any[]): boolean {
// Do what you want here
const id = this.constructor.name + ":" + event;
if (!items.has(id)) {
items.add(id);
console.log(id);
}
// And then call the original
return originalEmit.call(event, ...args);
}
You can very easily extend this and filter based on event name or class name.
You might want to look into RPC modules for node.js. If I am not mistaken the Dnode RPC module has an chat server/client example similar to what you are trying to do. So you could either make use of their module or copy what they are doing.
In brief the example shows a server which on connection creates listeners for all the server events from the connected client. It does this by simply iterating over a stored list of event names.
var evNames = [ 'joined', 'said', 'parted' ];
con.on('ready', function () {
evNames.forEach(function (name) {
emitter.on(name, client[name]);
});
emitter.emit('joined', client.name);
});
This code is clever because it automatically calls a remote procedure call on the client associated with the event when the event is emitted.