oninross
oninross

Reputation: 999

Heroku + ExpressJS + socket.io = Application Error

I have a web app hosted in Heroku, doing some simple chat using socket.io. However, when I deploy the codes to Heroku, i receive the following error:

at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=project-interactive-wall.herokuapp.com request_id=e98ea8c9-2cc0-47e5-a9bb-4a0d863ac712 fwd="14.202.200.120" dyno= connect= service= status=503 bytes= protocol=https

this is the code that I have in Heroku:

var app = require('express')(),
    http = require('http').Server(app),
    io = require('socket.io')(http);

app.get('/', function (req, res) {
    res.sendFile(__dirname + '/client/index.html');
});

io.on('connection', function (socket) {
    console.log('a user connected');

    socket.on('disconnect', function () {
        console.log('user disconnected');
    });

    socket.on('chat message', function (msg) {
        io.emit('chat message', msg);
    });
});

http.listen(3000, function () {
    console.log('listening on *:3000');
});

Any ideas? Heroku documentation is not much help.

Upvotes: 1

Views: 882

Answers (1)

Gerardo
Gerardo

Reputation: 979

Heroku sets a PORT environment variable which is needed in your code to run the server.

Just add process.env.PORT to http.listen() to get it work.

http.listen(process.env.PORT || 3000, function() {
  // ...
})

Upvotes: 1

Related Questions