title: Raising a socket.io event from a Restify handler
body: <p>I have a resitfy server and socketio server configured as per the example <a href="http://mcavage.me/node-restify/#socketio" rel="nofollow">here</a>. 
  My requirement is to raise a socket.io event from within a handler. So, my code looks like this:</p>
  
  <pre><code>var restify = require('restify');
  var socketio = require('socket.io');
  
  var server = restify.createServer();
  
  server.get('/', function (req, res, next) {
    res.json(204, null);
    io.emit("sample", {"sample":"event"});
    next();
  });
  
  server.listen(8080, function () {
      console.log('socket.io server listening at %s', server.url);
  });
  </code></pre>
  
  <p>My client code is pretty simple and looks like this:</p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;Socket.IO Chat Example&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;script src="socket.io.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript"&gt;
      var socket = io.connect('http://localhost:8080');
      socket.on('sample', function (data) {
        console.dir(data);
      });
    &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>When I start the server, the server exists after the first time a client connects and this is found in the logs:</p>
  
  <pre><code>  socket.io:server initializing namespace / +0ms
    socket.io:server creating engine.io instance with opts {"path":"/socket.io"} +3ms
    socket.io:server attaching client serving req handler +1ms
  socket.io server listening at http://0.0.0.0:8080
    socket.io:server incoming connection with id U9hLZRlbsGfvI8QPAAAA +2s
    socket.io:client connecting to namespace / +4ms
    socket.io:namespace adding socket to nsp / +0ms
  
  http.js:690
      throw new Error('Can\'t set headers after they are sent.');
            ^
  Error: Can't set headers after they are sent.
      at ServerResponse.OutgoingMessage.setHeader (http.js:690:11)
      at ServerResponse.format (/home/vagrant/socket.io-server-example/node_modules/restify/lib/response.js:109:10)
      at ServerResponse.send (/home/vagrant/socket.io-server-example/node_modules/restify/lib/response.js:231:24)
      at emitRouteError (/home/vagrant/socket.io-server-example/node_modules/restify/lib/server.js:152:13)
      at onRoute (/home/vagrant/socket.io-server-example/node_modules/restify/lib/server.js:610:21)
      at Router.find (/home/vagrant/socket.io-server-example/node_modules/restify/lib/router.js:512:5)
      at Server._route (/home/vagrant/socket.io-server-example/node_modules/restify/lib/server.js:604:21)
      at routeAndRun (/home/vagrant/socket.io-server-example/node_modules/restify/lib/server.js:572:14)
      at Server._handle (/home/vagrant/socket.io-server-example/node_modules/restify/lib/server.js:592:9)
      at Server.onRequest (/home/vagrant/socket.io-server-example/node_modules/restify/lib/server.js:255:14)
  </code></pre>
  
  <p>What I am not able to understand is, how can I serve REST API and socket.io events from the same port using restify?
  Why does restify complain about not being able to set headers once they are sent?</p>
  
  <p>Am I missing something fundamental here?</p>