Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an EventEmitter listen to another EventEmitter in Node.js?

I want to do something like this:

var events = require("events");

var emitterA = new events.EventEmitter();
var emitterB = new events.EventEmitter();

emitterA.addListener("testA", function(){
    console.log("emitterA detected testA");
});

emitterB.addListener("testA", function(){
    console.log("emitterB detected testA");
});

emitterA.emit("testA");

And the output to be this:

emitterA detected testA
emitterB detected testA

But when I run this code, the output I get is:

emitterA detected testA

I basically want one emitter to listen to events emitted by another emitter.

The Rationale: I'm writing a server on Node, and the server sends out Server Sent Events. This means I'll require persistent connections. To ensure the connection isn't closed for some reason (I don't want to set the timeout to infinity because that feels like a hack and doesn't seem safe), I send out a heartbeat with just blank data, just so something is being passed to the browser.

I'm running one timer, and every time period (1 second in my test case), it will trigger server.emit("hb"); after which I write and send data to all request objects simultaneously (on my laptop, that's just multiple tabs and multiple browsers). So basically req.on("hb", callback)

This seems cleaner, to me, because the alternative is to give each request object its own timer, meaning there will be many many timers running all over the place, each one causing their respective request objects to emit heartbeat events. It seems like a non-optimal way to do things.

Plus, because each request object is spontaneously created and destroyed, doing it that way will basically ensure that the listeners are created and destroyed too.

So what I want is on a heartbeat event emitted by the server, all request objects active will hear it and write data along their own connections.

Another alternative (the one I've gotten working right now) is to make the server listen to its own events, and make the server write the heartbeats. The problem with this is the maxListeners limit on the server -- every request object will append a new "hb" listener to the server so the server can listen on that event for that specific request object. The maximum listeners, though, is 10, and while I can set this to infinity as well, I'm not too keen on doing that as I'm genuinely curious if there could be a better method.

The best and cleanest solution to me really seems to be making the request objects "subscribe" to the server events. I'm doing this to learn how to program in node, so I'd like to implement as little external libraries as possible and do it with node's raw power, but if it takes an external library, I'd be happy to read its source code just so I can learn how to implement a small local implementation.

like image 747
markovchain Avatar asked Oct 20 '14 12:10

markovchain


People also ask

How do I add a listener to EventEmitter?

on(event, listener) and eventEmitter. addListener(event, listener) are pretty much similar. It adds the listener at the end of the listener's array for the specified event. Multiple calls to the same event and listener will add the listener multiple times and correspondingly fire multiple times.

How can we add an event listener to an event type NodeJS?

The once(eventName, fn) methodAdds a one-time listener function for the event named eventName . The next time eventName is triggered, this listener is removed and then invoked. Use for setup/init kind of events.

Is EventEmitter synchronous?

When the EventEmitter object emits an event, all of the functions attached to that specific event are called synchronously. Any values returned by the called listeners are ignored and discarded. The following example shows a simple EventEmitter instance with a single listener.

How do I trigger an event in NodeJS?

Emitting events: Every event is named event in nodejs. We can trigger an event by emit(event, [arg1], [arg2], […]) function. We can pass an arbitrary set of arguments to the listener functions.


1 Answers

Note that EventEmitters are kind of sole-source in javascript. They are not system-wide events that everyone can listen to. They are objects that can emit events to support the asynchronous patterns. To accomplish what you want, you need multiple things listening to the same emitter. Something like this:

'use strict';
var events = require( 'events' );
//Create a new sole-source event emitter
var emitterA = new events.EventEmitter();

//create a container that can listen
function EventListener( name ) {
  console.log( 'new event listener, name=' + name );
  this.name = name;
  this.ack = function() {
    console.log( this.name + ' just heard testA' );
  };
  this.listenTo = function( event, emitter ) {
    var self = this;
    emitter.on( event, function() {
      self.ack();
    } );
  };
}

var listenerA = new EventListener( 'A', emitterA );
listenerA.listenTo( 'testA', emitterA );

var listenerB = new EventListener( 'B', emitterA );
listenerB.listenTo( 'testA', emitterA );

setInterval( function() {
  emitterA.emit( 'testA' );
}, 1000 );

Outputs:

$ node testfile.js 
new event listener, name=A
new event listener, name=B
A just heard testA
B just heard testA
A just heard testA
B just heard testA
...

Note that for your additional use cases, you will want to use WebSockets. EventEmitters will not work the way you want them to, especially to remote clients. (The comment for socket.io is indeed a good place to start.)

like image 154
clay Avatar answered Oct 13 '22 00:10

clay