NodeJS and socket.io app on Heroku with 2 ports
<p>My app has an express server listening on one port (process.env.PORT) and I also want a web socket using another port. I used to use Express 3 with this set-up:</p>

<pre><code>var express         = require('express'),
    http             = require('http'),
    io              = require('socket.io'),
    app             = express();
    server          = http.Server(app);
    ioServer        = io(server);

ioServer.on('connection', callback);
server.listen(process.env.PORT || 3000, function () {
    console.log('App listening on ' + server.address().port);
});
</code></pre>

<p>The above code worked fine, as when creating ioServer, no specific port is required. However, after I switched to Express 4 and started using Heroku's WebSocket service, I had to specify a port like this:</p>

<pre><code>var WebSocketServer = require('ws').Server,
    port = 5000,
    server = http.createServer(app);
    server.listen(port);

var wss = new WebSocketServer({server: server});

wss.on('connection', callback);

app.listen(app.get('port'), function() {
    console.log('Express server listening.'));
});
</code></pre>

<p>This new set-up never works because when I run the app on Heroku, I get an error complaining that the same port can't be used twice:</p>

<pre><code>Error: listen EADDRINUSE :::40854
</code></pre>

<p>The set-up logic is essentially the same except for explicitly assigning a port in Express 4, so why did my code work with Express 3 but not Express 4? How should I fix this?</p>