Running an IRC bot on Heroku - node.js

I've got my bot up and running, and I would like it to run on Heroku to keep it persistently connected to our IRC-channel. This is the content of my procfile:
web: coffee marvin.coffee
(The bot's name is marvin).
And this is marvin.coffee
irc = require 'irc'
config = require('./config').config
client = new irc.Client(config.server, config.nick, config.options)
# IRC-listeners
I'm never creating any HTTP server, and I was hoping that it was possible to simply run this coffeescript on Heroku in order for my bot to run, but I get the following error:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
The bot is launched, and joins the channel, but immediately leaves due to the fact that Heroku kills the process due to this error.
Any help would be greatly appreciated :-)

Does it work if you change web: coffee marvin.coffee to bot: coffee marvin.coffee in your Procfile?

Related

node js server deployment on heroku

we are working on a video call application, we used nodejs and socket io for the server. We have arrived at the deployment phase. When deploying the server on heroku it displays Build succeeded but when I launch the link or I click on open app leads to a white page where is written :
Cannot GET /
Any help please
I tried to replace the port process.env.SOCKET_BACKEND_URL || "http://localhost:5000"
bye by the link given while deployment:
process.env.SOCKET_BACKEND_URL ||"https://appname.herokuapp.com/"

Make nodemon auto-restart a program on crash without waiting for file changes at custom error?

I'm building an E-commerce site, where there's an Authentication system.
I noticed that if the client login with a wrong user or password, the backend/server that works with nodemon will crach and hang in there crashed till i restart manually nodemon. This is example output error of the nodemon crash:
[nodemon] app crashed - waiting for file changes before starting...
node:internal/errors:464
ErrorCaptureStackTrace(err);
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent
to the client
Ofcourse, when server crashes, client can no more access or do login again till server restarts.
After some googling, i found this question and this repository that fix my problem but particulary and not as expected precisely, i dont want nodemon to restart forever on any error that occure ofcourse, but only with specifics errors that i set them -like Authentication errors as i mentionned above-.
So, my idea/question is: is there anyway to get nodemon restarts by itself in some cases of failures or errors (NOT ALL)?
Seems like you a referring to a production situation, and nodemon is a development node server, which is not intended for use in production, as the intro states:
nodemon is a tool that helps develop Node.js based applications by
automatically restarting the node application when file changes in the
directory are detected.
You should use node.js in production, instead of nodemon.
For managing your node server in production, you could use a process manager like PM2..
That said, an authentication server that crashes every time a user uses a wrong password seams very ineffective in handling a common use case. So I would advise to start with fixing the root cause, which is the buggy server, and then for recovery from incidental crashes use something like PM2.
PS:
The error you are getting looks like an express error you get when you send a response (in this case an error response) without exiting the function e.g. by using return. Because you are not returning, another res.send is called, which causes the 'ERR_HTTP_HEADERS_SENT' error. See this answer.
This is really bad since it can send your program into a loop of restarting, but if you really want it, replace app.js with your file's name and try this:
nodemon -x 'node app.js || copy /b app.js +,,'
Linux version:
nodemon -x 'node app.js || touch app.js'
Next time try a little googleing before you ask since it is most likely faster.

Why does my Heroku Discord bot crash after it can't connect to a port? [duplicate]

This question already has answers here:
Heroku Node.js Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
(6 answers)
Closed 3 years ago.
I'm trying to make a discord bot that runs 24/7 using Heroku. Everything is good, except for the fact that the bot crashes after 60 seconds.
The error output tells me this:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
I looked for solutions on the internet, and I found quite a few. However, none of them worked.
Here's my main file's code:
const Discord = require('discord.js')
const {prefix, token} = require('./config.json')
const client = new Discord.Client()
// Login
client.login(token)
client.once('ready', () => {
console.log('Back online')
client.user.setActivity(' channel', {type: 'LISTENING'})
})
client.on('message', message => {
if (message.author.bot) {
return;
}
if (message.content.toLowerCase() === 'hello') {
message.channel.send('Hey!')
}
})
You've most likely assigned your bot to run on a web service in your Procfile. The Heroku Procfile is a file that stores information about what processes Heroku should run. If you set your Procfile to run a web service, Heroku expects you to bind to the required port (using process.env.PORT) once it launched. If it didn't then Heroku will assume that your program failed to start and will restart it.
As of now, your Procfile most likely looks like this:
web: node index.js
This tells Heroku to run your program in a web dyno. However, if you don't bind to an HTTP port with a Node.js service like Express, Heroku will crash your program. To fix this, change web to worker.
worker: node index.js
Note that by changing your Procfile to use worker, your free dyno hours (if you are running on a free dyno) will continue decreasing 24/7, and you'll be using up around 700 hours per month. If you registered your credit card, the limit is set to 1000 hours per month and you don't have to worry. Otherwise, you have to upgrade your dyno to a Hobby dyno to keep your bot running the entire month.
EDIT: Though this wasn't the accepted answer, I still have to clarify that sometimes Heroku doesn't read the Procfile settings. In that case, you should run these commands in the folder where your project is:
heroku ps:scale web=0
heroku ps:scale worker=1
This will force Heroku to use the worker dyno defined in your Procfile. Hope this helps.

Heroku open is not working when working with node / express app

I am getting an error when trying to run "heroku open". It is saying that it tries to run the application with npm start but then "Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch" happens. I am using macOS with npm jazz.
Here is a pic of my logs.
At first i thought it was because i was not adding "start" in my package.json but that didnt seem to work either.

Starting my Node.js + Mongo API with Heroku

I know it's maybe a beginner question, but I looked over it everywhere and I still having trouble with it.
I have a Node API that I run locally. The only thing I need to do is run in terminal:
$ mongod
and then
$ node src/loader.js
So, I upload my code to Heroku and added to my package.json:
"scripts": {
"start": "node src/loader.js"
}
But when I run '$ heroku ps' it says that my process crashed. Whats the difference between my local build and the Heroku build?
Adding my Heroku logs --tail.
2018-02-05T19:19:17.648257+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2018-02-05T19:19:17.648257+00:00 heroku[web.1]: Stopping process with SIGKILL
2018-02-05T19:19:17.732544+00:00 heroku[web.1]: Process exited with status 137
Start by having a look over the heroku logs from terminal 'heroku logs --tail', and if you need more detail you can go to the app in heroku and in the upper right view the logs in the heroku platform.
If you can post some of the logs it will be helpful in telling what's going wrong if you can't derive it from there on your review.
So, I discovered the problem. Heroku dynamically assigns your app a port, so you can't set the port to a fixed number, and in my code I assigned to port: '3000'.
So you need to change to
var port = process.env.PORT || 3000;

Resources