title: Node.js: how to send events across modules
body: <p>I created two modules, "person" and "group", with "group" depending on "person":</p>
  
  <p>person.js:</p>
  
  <pre><code>var events = require('events');
  
  exports.Person = function() {
      var personEvents = new events.EventEmitter();
  
      function generateEvent(args) {
          console.log("Emitting event with args "+JSON.stringify(args));
          personEvents.emit("myEvent", args);
      }
  
      return {
          personEvents: personEvents,
          generateEvent: generateEvent
      };
  }
  </code></pre>
  
  <p>group.js:</p>
  
  <pre><code>var PERSON = require("person.js").Person();
  var events = require('events');
  
  exports.Group = function() {
      function handleEvent(args) {
          console.log("Caught event with args "+JSON.stringify(args));
      }
  
      PERSON.personEvents.on("myEvent", function(args) {
          handleEvent(args);
      });
  }
  </code></pre>
  
  <p>Now I have the main program main.js as a separate module:</p>
  
  <pre><code>var PERSON = require("person.js").Person();
  var GROUP = require("group.js").Group();
  
  PERSON.generateEvent({ type: "bla" });
  </code></pre>
  
  <p>When I execute "node ./main.js", I will see the message that the event was generated, but the event is not caught.  It seems that the listener in the group module is not bound to the same "personEvents" variable created in the person module in the main namespace.</p>
  
  <p>I am rather new at javascript and its implications for programming.  I used examples I found on the internet to get the code I have so far, but I feel that this may not be the correct way to setup the modules and the cross-module events.</p>
  
  <p>Can someone explain to me how I should setup the modules so I can generate events in one module and catch them in another?
  Thanks!</p>