title: What does addListener do in node.js?
body: <p>I am trying to understand the purpose of addListener in <a href="http://nodejs.org/" rel="nofollow noreferrer">node.js</a>. Can someone explain please?  A simple example would be:</p>
  <pre><code>var tcp = require('tcp');
  var server = tcp.createServer(function (socket) {
    socket.setEncoding(&quot;utf8&quot;);
    socket.addListener(&quot;connect&quot;, function () {
      socket.write(&quot;hello\r\n&quot;);
    });
    socket.addListener(&quot;data&quot;, function (data) {
      socket.write(data);
    });
    socket.addListener(&quot;end&quot;, function () {
      socket.write(&quot;goodbye\r\n&quot;);
      socket.end();
    });
  });
  server.listen(7000, &quot;localhost&quot;);
  </code></pre>