title: Custom Events in Node.js with Express framework
body: <p>So, I'd like to know how to create custom events in node.js, and I'm hitting a wall. I'm pretty sure I'm misunderstanding something about how express works and how node.js events work.</p>
  
  <p><a href="https://creativespace.nodejitsu.com">https://creativespace.nodejitsu.com</a> That's the app.</p>
  
  <p>When a user creates a new "activity" (something that will happen many times) they send a POST request. Then within my route, if that POST succeeds I'd like to emit an event, that tells socket.io to create a new namespace for that activity.</p>
  
  <p>In my route file:</p>
  
  <pre><code>var eventEmitter = require('events').EventEmitter;    
  // Tell socket.io about the new space.
  eventEmitter.emit('new activity', {activityId: body.id});
  </code></pre>
  
  <p>And socket.io:</p>
  
  <pre><code>// When someone creates a new activity
  eventEmitter.on('new activity', function (data) {  // this gives and error
    var newActivity = '/activity?' + data.activityId;
    io.of(newActivity).on('connection', function (socket) {
  
      // Socket.io code for an activity
  
    });
  });
  </code></pre>
  
  <p>So the error I get is CANNOT CALL METHOD ON OF UNDEFINED and it refers to what would be line 2 in the socket.io above. I think I'm messing up my requires, maybe...or I'm not quite understanding how events work.</p>
  
  <p>Any help, even a reference to good reading on Node.js events would rock!</p>
  
  <p>Thanks!!!</p>