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

送分小仙女□ 提交于 2019-12-07 09:31:57

问题


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.


回答1:


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.)




回答2:


You can try to emit the event forward to the element emitterB:

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

    // When everything is done emit the event to emitterB
    emitterB.emit('testA');
});

Or if there are multiple events to emit forward you can create something like a event bubbling:

function bubbleEvents(from, to) {

    var oldEmit = from.emit;

    from.emit = function (event) {

        var args = Array.prototype.slice.call(arguments, 1),
            newArgs = [event].concat(args);

        oldEmit.apply(from, newArgs);
        to.emit.apply(to, newArgs);
    };
}

// and call it
bubbleEvents(emitterA, emitterB);

P.S. It is not a good practice to edit the methods of an object like this, but for some dirty purposes it can serve very well.



来源:https://stackoverflow.com/questions/26465358/how-to-make-an-eventemitter-listen-to-another-eventemitter-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!