title: Socket.io + NodeJS doesn't work on Heroku
body: <p>I am doing an API and it's on Heroku. But I am having some problems with the socket.io only on the heroku side, when I test it in local everything goes fine. The API is completely independent of the frontend, so they are in a different domains (and a different hosts). The problem is that on production, I don't get succeed in connect the sockets...</p>
  
  <p>I have some questions, all of that are about the socket.io configuration on heroku. I know that there are some posts with some information about that, but the posts I found it was with old versions of sockets.io or old versions of heroku (heroku seems to has changed the websockets stuff the past July):</p>
  
  <ul>
  <li><p>I don't know if I need to activate something before run socket.io on heroku. I read some posts about that, but all seems to be old... I tried to activate Websockets with: <code>$ heroku labs:enable websockets</code> but the response that I got it was: <code>!    No such feature: websockets</code>.</p></li>
  <li><p>Do I have to specify a port, or Heroku has an automatic port for that?</p></li>
  <li><p>Do I need two connections? One to listen the POST/GET/PUT/DELETE and another to the sockets?</p></li>
  </ul>
  
  <p><strong>app.js</strong></p>
  
  <pre><code>    var express = require('express');
      var app = module.exports = express();
      var port = process.env.PORT || 5000;
      var port_s = 3000;
      var server = require('http').createServer(express);
      ...
      app.listen(port);
      server.listen(port_s);
  
      require('./config/socket-io')(app, server, secret);
      app.post('/user', routes.users.register);
      ...
  </code></pre>
  
  <p><strong>socket-io.js</strong></p>
  
  <pre><code>module.exports = function(app, server, secret) {
      var clients = {};
      console.log("initiating sockets...");
      var sio = require('socket.io').listen(server);
  
      sio.sockets.on('connection', function (socket) {
          clients[socket.id] = socket;
          console.log("...new connection: "+socket.client.id);
          socket.emit('identification', { data : socket.client.id });
  
          socket.on('newShoutOut', function(data) {
              var receptor    = data.idTo;
              var emiter      = socket.client.id;
              //console.log("...new shout out from " +emiter+ " to "+receptor);
              var elem = findElement(sio.sockets['sockets'], 'id', receptor);
              sio.sockets.sockets[elem].emit('privateShoutout',{ data : data.data, from : emiter });
          });
  
          socket.on('disconnect', function() {
              //console.log("..."+socket.client.id + " disconnected");
          });
      });
  };
  
  function findElement(arr, propName, propValue) {
      for (var i=0; i &lt; arr.length; i++) {
          if (arr[i].id === propValue)
              return i;
      };
  }
  </code></pre>
  
  <p>I repeat, everything works on localhost. I tried the API on localhost:5000 and the client app on localhost:80 and all the sockets work fine. </p>
  
  <p>Thank you.</p>