Why use event emitters in node js

Listen to all broadcast events in Node.js.

Is there a way in Node.js all listen to events output by an EventEmitter object?

e.g. you can do something like ...

The idea is that I want all the events spewed out by a server side to capture the event data, send it over a websockets connection, reform it as an event on the client side, and then act on the event on the client side.


As mentioned earlier, this behavior is not at the core of node.js. However, you can use EventEmitter2 from hij1nx:


EventEmitter does not break any existing code, but it does support namespaces and wildcards. For example:

I know this is a bit old but what the heck, here's another solution you could go for.

You can easily add a monkey patch to the emit function of the emitter you want to use to capture all events:

This is pretty simple code that should work on any emitter.

With ES6 classes, it's very easy:

Note that all of the solutions described above that may work involve some kind of hacking for the internal implementation of node.js EventEmitter.

The correct answer to this question would be: The standard EventEmitter implementation does not support this. You have to get around them .

If you look at the source code of node.js for EventEmitter, you will notice that a listener that is not associated with a specific event type is returned with no further action as it tries to get the callback function from a hash based on the event type:


Therefore, something like this cannot work by default.

As of Node.js v6.0.0, the new syntax and argument distribution operator is fully supported, so it's fairly safe and fairly easy to implement the functionality you want with simple inheritance and method override:

This implementation is based on the fact that the original method of return / depends on whether the event was handled by a listener or not. Notice that there is a statement in the override, so we'll keep this behavior for other consumers.

The idea here is to use the star event () to create handlers that run for each individual event (e.g. for logging purposes) and for the empty event (e.g.) for a standard or catch all handler if nothing else intercepts this event.

We make sure that the star () event is called first because for events without a handler the result is actually an exception that is thrown. For more information, see the implementation of the.

For example:

If an EventEmitter instance already exists, but you want to adapt that particular instance to the new behavior, you can do so easily by patching the method at runtime as follows:

This is based on the answer Martin gave above. I'm a bit new to the knot so I had to figure out its answer for myself. The method at the end, logAllEmitterEvents, is the important bit.

I had to keep track of all the issued events in all of the libraries, so I tapped that.

This example uses a, but you can just remove it if you don't care about that type of nonsense.

Refers within the call to the object that is emitting. It was very easy to keep track of all of the unique objects: emits in my project.

You can very easily expand this and filter based on the event name or class name.

You may want to examine RPC modules for node.js. If I'm not mistaken, the Dnode-RPC module has a chat server / client sample that is similar to what you are trying. So you can either use their module or copy what they do.

The example briefly shows a server that, when connected, creates listeners for all server events from the connected client. This is done simply by going through a stored list of event names.

This code is clever because when the event is triggered, it automatically makes a remote procedure call on the client associated with the event.

Encountered the same problem today, here is a solution:

A monkey patch adds any method to EventEmitter.

It is useful to be able to monitor only the events of a problem.

Here is a debug tool inspired by Martin's answer (https://stackoverflow.com/a/18087021/1264797). I just used this to find out what went wrong on a number of streams by logging all the events on the console. Works perfectly. As Martin shows, OP could use it by replacing the console.log () call with a websocket sender.

You can also use another event emitter implementation like https://github.com/ozantunca/DispatcherJS. The implementation would be as follows:

DispatcherJS also supports namespaces and even dependencies to determine which callbacks are invoked first.

We use cookies and other tracking technologies to improve your browsing experience on our website, to show you personalized content and targeted ads, to analyze our website traffic, and to understand where our visitors are coming from.

By continuing, you consent to our use of cookies and other tracking technologies and affirm you're at least 16 years old or have consent from a parent or guardian.

You can read details in our Cookie policy and Privacy policy.