Listen to All Emitted Events in Node.js

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.


With ES6 classes it's very easy:

class Emitter extends require('events') {
    emit(type, ...args) {
        console.log(type + " emitted")
        super.emit(type, ...args)
    }
}

Be aware that all solutions described above will involve some sort of hacking around node.js EventEmitter internal implementation.

The right answer to this question would be: the default EventEmitter implementation does not support that, you need to hack around it.

If you take a look on node.js source code for EventEmitter, you can see listeners are retrieved from a hash using event type as a key, and it will just return without any further action if the key is not found:

https://github.com/nodejs/node/blob/98819dfa5853d7c8355d70aa1aa7783677c391e5/lib/events.js#L176-L179

That's why something like eventEmitter.on('*', ()=>...) can't work by default.