title: How to make an EventEmitter listen to another EventEmitter in Node.js?
body: <p>I want to do something like this:</p>
  
  <pre><code>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");
  </code></pre>
  
  <p>And the output to be this:</p>
  
  <pre><code>emitterA detected testA
  emitterB detected testA
  </code></pre>
  
  <p>But when I run this code, the output I get is:</p>
  
  <pre><code>emitterA detected testA
  </code></pre>
  
  <p>I basically want one emitter to listen to events emitted by another emitter.</p>
  
  <p>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.</p>
  
  <p>I'm running one timer, and every time period (1 second in my test case), it will trigger <code>server.emit("hb");</code> 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 <code>req.on("hb", callback)</code></p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>