1

I have a simple Node.js app hosted on Heroku that consumes a Twitter stream. It works fine sometimes, but when I review my logs, I see it has begun to crash regularly with an R10 error code.

I've done a good deal of googling for other questions on this topic, like this one, and the consensus seems to be that Heroku requires you to listen to process.env.PORT instead of explicitly setting your own port. Fair enough.

But I'm not setting a port anywhere in my code, and nor do I set up an http.createServer where I can .listen to one. So how can I solve this error? Any ideas?

Here's my code for reference:

var config = require('./config');
var count = require('./count');
var Twit = require('twit'); 
var T = new Twit(config);

port = process.env.PORT;  // this is just for reference, not used anywhere

var stream = T.stream('user', {track: "@isitoveryet2016"});

stream.on('connect', function (request) {
    console.log("Opening Twitter stream...");
}); 

stream.on('connected', function (response) {
    console.log("Connected and streaming on port: " + port);
});

stream.on('tweet', function (tweet){
    if (tweet.text.charAt(0) == "@"){
        if (tweet.user.screen_name == "isitoveryet2016") {
        console.log("Ignoring my own reply.");
        } else {
        var thisTweet = count.TimeLeft();
        console.log("Received direct tweet from: " + tweet.user.screen_name);
        console.log("Text: " + tweet.text);
        T.post('statuses/update', {in_reply_to_status_id: tweet.id_str, status: "@" + 
        tweet.user.screen_name + 
        thisTweet}, function (err, data, res){
            if (err) return handleError(err);
            else console.log('tweet deployed!');
            });
        }
    }
    else {
        console.log("Not a direct tweet, not replying.");
    }
});

stream.on('warning', function (response) {
    console.log(response);
});

stream.on('disconnect', function (response) {
    console.log(response);
});

function handleError(err) {
  console.error('response status:', err.statusCode);
  console.error('data:', err.data);
}
Community
  • 1
  • 1
ntang
  • 13
  • 3

1 Answers1

1

By default, you have a web process that's expected to listen on port 80. You can change that to a non-web process by creating a Procfile, like:

worker: node twitter.js

Then telling Heroku to use just your worker, and to not expect a web process:

heroku scale web=0 worker=1

(at least, I'm fairly certain that should do what you want. It's very rare for apps not to have a web server involved somewhere)

(also: you don't need to do the process.env.PORT thing if you don't plan on using it. We don't look at your code, we just expect any web process to eventually bind to $PORT)

disclosure: I'm the Node.js Platform Owner at Heroku.

hunterloftis
  • 13,386
  • 5
  • 48
  • 50
  • Thanks! I tried that, and it works, but now it seems to be duplicating my app. In the logs, I'm now getting 2x the console responses: app[worker.1] "Console response" app[run.123] "Console response" app[run.456] "Console response" I'm a little confused. Am I duplicating my own processes here? – ntang Aug 07 '15 at 20:24
  • If you only see app[worker.1] (and nothing like app[worker.2] or app[web.1]) then you've only got a single instance of your app running. – hunterloftis Aug 07 '15 at 20:27
  • Okay... so I now seem to be running both a [worker.1] instance AND a app[run.1234] instance of my application. I can manually run `heroku stop` to stop the app instance, but is there a way to do it automatically? – ntang Aug 07 '15 at 20:55
  • `app[run.1234]` is created when you run a one-off command like `heroku run bash`. – hunterloftis Aug 07 '15 at 21:00