romainlap
romainlap

Reputation: 13

My socket.io app's server is not starting on Heroku

I'm trying to deploy my app on Heroku. I've followed these instructions: https://devcenter.heroku.com/articles/getting-started-with-nodejs, enables websockets, but when I run $heroku open, the I land on the error page.

Here are the logs I get from heroku:

2014-07-14T11:41:27.331343+00:00 heroku[web.1]: Starting process with command `node index.js`
2014-07-14T11:41:28.431660+00:00 app[web.1]: info: socket.io started
2014-07-14T11:42:27.710153+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2014-07-14T11:42:27.710206+00:00 heroku[web.1]: Stopping process with SIGKILL
2014-07-14T11:42:28.915226+00:00 heroku[web.1]: Process exited with status 137
2014-07-14T11:42:28.927884+00:00 heroku[web.1]: State changed from starting to crashed

Here is the index.js file of my app:

// Import the Express module
var express = require('express');

// Import the 'path' module (packaged with Node.js)
var path = require('path');

// Create a new instance of Express
var app = express();

// Import Anagrammatix game file.
var agx = require('./game');

// Create a simple Express application
app.configure(function() {
    // Turn down the logging activity
    app.use(express.logger('dev'));

    // Serve static html, js, css, and image files from the 'public' directory
    app.use(express.static(path.join(__dirname,'public')));
});

// Create a Node.js based http server on port 8080
var server = require('http').createServer(app).listen(8080);

// Create a Socket.IO server and attach it to the http server
var io = require('socket.io').listen(server);

// Reduce the logging output of Socket.IO
io.set('log level',1);

// Listen for Socket.IO Connections. Once connected, start the game logic.
io.sockets.on('connection', function (socket) {
    //console.log('client connected');
    agx.initGame(io, socket);
});

The node_modules I'm using are: - express - mysql - socket.io

Do you know what I should change to get the app work? Let me know if you need me to provide more info.

Thanks!

Upvotes: 1

Views: 1727

Answers (1)

Alex P
Alex P

Reputation: 6072

Your code is trying to bind to a hard-coded port 8080, but Heroku will tell your app which port to connect to. Heroku uses this to detect that your app's started correctly, as well as for routing. Your code's not starting on that port, though, so it gets killed with the error "Web process failed to bind to $PORT within 60 seconds of launch". Heroku doesn't know that your app has started, and it doesn't know which port it's listening to.

It's an easy fix, though. This will tell your app to connect to the port specified in the PORT environment variable, or 8080 if that variable isn't set:

var server = require('http').createServer(app).listen(process.env.PORT || 8080);

Upvotes: 3

Related Questions