title: Node.js - emit vs function call
body: <p>I have been been using EventEmitters in Node and come across certain situations where I can't decide whether to emit values back into the 'EventEmitter object' and have it listen for them, or simply call a function to set the value.</p>
  
  <p>I have created a simple example to explain my point.</p>
  
  <p>The myTimer function simply emits whatever the favourite number is every second. It's value can be changed either through the <em>changenumber</em> function call or on a <em>changenumber</em> event.</p>
  
  <pre><code>var util = require("util");
  var events = require("events");
  var favNo = 2;
  
  var myTimer = function() {
      var self = this;
      events.EventEmitter.call(this);
  
      setInterval(function() {
          self.emit('favourite number', favNo);
      }, 1000);
  
      // on changenumber event, change number
      this.on('changenumber', function (newNo) {
          favNo = newNo;
      });
  }
  
  util.inherits(myTimer, events.EventEmitter);
  
  // on function call, change number
  myTimer.prototype.changenumber = function(newNo) {
      favNo = newNo;
  }
  module.exports = myTimer;
  </code></pre>
  
  <p>This code below uses the myTimer function and listens for it's 'favourite number' event.
  The favourite number can be set either with the function call or by emitting 'change number'  on the myTimer object.</p>
  
  <pre><code>var myTimer = require('./myTimer.js');
  var timer = new myTimer();
  
  timer.on("favourite number", function(number) {
      console.log(number);
  });
  
  timer.changenumber(102); // change number using function.
  timer.emit('changenumber', 102); // change number by emitting number
  </code></pre>
  
  <p>Both approaches seem to work, however using the emit to send back values seems like it may become hard to follow if the complexity grows. I have been using socket.io in my projects and have seen it basically emits and listens on the same object for both client and server ends, so maybe that's where I have become confused.</p>
  
  <p>Are there any situations where using one method may be more appropriate than the other?</p>