Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Error running a socket.io node.js app on Heroku

Error running a socket.io node.js app on Heroku

Problem

I've deployed succesfully but when I run my app I get this error in my browser.

enter image description here

These are what my heroku logs says:

C:\Users\Shekhar\heroku>heroku logs
2013-09-18T19:50:39.552663+00:00 heroku[api]: Enable Logplex by shekharsumanrock
@gmail.com
2013-09-18T19:50:39.574021+00:00 heroku[api]: Release v2 created by shekharsuman
[email protected]
2013-09-18T19:51:51+00:00 heroku[slug-compiler]: Slug compilation started
2013-09-18T19:52:12.810160+00:00 heroku[api]: Scale to web=1 by shekharsumanrock
@gmail.com
2013-09-18T19:52:12.838111+00:00 heroku[api]: Add PATH config by shekharsumanroc
[email protected]
2013-09-18T19:52:12.866467+00:00 heroku[api]: Release v3 created by shekharsuman
[email protected]
2013-09-18T19:52:12.914733+00:00 heroku[api]: Deploy 36856b3 by shekharsumanrock
@gmail.com
2013-09-18T19:52:12.943415+00:00 heroku[api]: Release v4 created by shekharsuman
[email protected]
2013-09-18T19:52:13+00:00 heroku[slug-compiler]: Slug compilation finished
2013-09-18T19:52:15.273489+00:00 heroku[web.1]: Starting process with command `n
ode app.js`
2013-09-18T19:52:15.912133+00:00 heroku[web.1]: Starting process with command `n
ode app.js`
2013-09-18T19:52:16.112156+00:00 app[web.1]: info: socket.io started
2013-09-18T19:52:16.112156+00:00 app[web.1]: Server running at http://localhost:
5000/
2013-09-18T19:52:19.292035+00:00 app[web.1]: Server running at http://localhost:
5000/
2013-09-18T19:52:19.292035+00:00 app[web.1]: info: socket.io started
2013-09-18T19:53:16.461372+00:00 heroku[web.1]: Stopping process with SIGKILL
2013-09-18T19:53:16.461211+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web
process failed to bind to $PORT within 60 seconds of launch
2013-09-18T19:53:17.545639+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web
process failed to bind to $PORT within 60 seconds of launch
2013-09-18T19:53:17.545980+00:00 heroku[web.1]: Stopping process with SIGKILL
2013-09-18T19:53:17.710781+00:00 heroku[web.1]: Process exited with status 137
2013-09-18T19:53:18.938730+00:00 heroku[web.1]: Process exited with status 137
2013-09-18T19:53:20.623319+00:00 heroku[web.1]: Starting process with command `n
ode app.js`
2013-09-18T19:53:22.046545+00:00 app[web.1]: info: socket.io started
2013-09-18T19:53:22.046545+00:00 app[web.1]: Server running at http://localhost:
5000/
2013-09-18T19:54:22.301236+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web
process failed to bind to $PORT within 60 seconds of launch
2013-09-18T19:54:22.301533+00:00 heroku[web.1]: Stopping process with SIGKILL
2013-09-18T19:54:24.052821+00:00 heroku[web.1]: Process exited with status 137
2013-09-18T19:53:17.722653+00:00 heroku[web.1]: State changed from crashed to st
arting
2013-09-18T19:53:17.721912+00:00 heroku[web.1]: State changed from starting to c
rashed
2013-09-18T19:54:35.879665+00:00 heroku[router]: at=error code=H10 desc="App cra
shed" method=GET path=/ host=stark-temple-8404.herokuapp.com fwd="117.198.13.71"
 dyno= connect= service= status=503 bytes=
2013-09-18T19:54:36.881441+00:00 heroku[router]: at=error code=H10 desc="App cra
shed" method=GET path=/favicon.ico host=stark-temple-8404.herokuapp.com fwd="117
.198.13.71" dyno= connect= service= status=503 bytes=

What mistake am I doing?

-------EDIT-------

app.js file

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')

function handler(req, res) {
    if ('GET' == req.method && '/images' == req.url.substr(0, 7) && '.jpg' == req.url.substr(-4)) {
        fs.stat(__dirname + req.url, function (err, stat) {
            if (err || !stat.isFile()) {
                res.writeHead(404);
                res.end('Not Found');
                return;
            }
        serve(__dirname + req.url, 'application/jpg');
    });
    } 
    else if ('GET' == req.method && '/' == req.url) {
        serve(__dirname + '/index.html', 'text/html');
    } 
    else if ('GET' == req.method && '.css' == req.url.substr(-4)) {
        serve(__dirname + req.url, 'text/css');
    }
    else if ('GET' == req.method && '.ico' == req.url.substr(-4)) {
        console.log("ICON has been called");
        serve(__dirname + req.url, 'image/x-icon');
    } 
    else if ('GET' == req.method && '.js' == req.url.substr(-3)) {
        serve(__dirname + req.url, 'application/javascript');
    } 
    else if ('GET' == req.method && '.png' == req.url.substr(-4)) {
        serve(__dirname + req.url, 'image/png');
    }
    else {
        res.writeHead(404);
        res.end('Not found');
    }
    function serve (path, type) {
        res.writeHead(200, { 'Content-Type': type });
        fs.createReadStream(path).pipe(res);
    }
};

console.log('Server running at http://localhost:5000/');
app.listen(5000);


io.configure(function () { 
  io.set("transports", ["xhr-polling"]); 
  io.set("polling duration", 10); 
});

io.sockets.on('connection', function (socket) {
  io.sockets.emit('this', { will: 'be received by everyone'}); //This will be received by everyone
  socket.emit('news', { hello: 'world' }); //This will be received by none but one
  socket.on('my other event', function (data) {
    console.log(data);
  });
  socket.on('disconnect', function () {
    io.sockets.emit('user disconnected');
  });
  socket.broadcast.emit('user connected'); //This will be received by everyone but one who initiates it



  socket.on('set nickname', function (name) {
    socket.set('nickname', name, function () {
      socket.emit('ready');
    });
  });

  socket.on('msg', function () {
    socket.get('nickname', function (err, name) {
      console.log('Chat message by ', name);
    });
  });
});

index.html file




YAY!!
What's up?
Problem courtesy of: Shekhar

Solution

Your process takes too long to start up. Hard to say without looking at your code, but I am guessing you are not binding to the right port. Are you listening on the port specified by the environment variable PORT? That's how Heroku detects when you have finished "starting up".

Solution courtesy of: Nitzan Shaked

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Error running a socket.io node.js app on Heroku

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×