How to share a database on heroku

January 9, 2013

Recently i developed my own service for Website monitoring. This Service is devided into two parts. The first part is a Ruby on Rails application, that is responsible for the user registration and creating new domains to monitor. The second part is a Node.JS application that checks the state of all domains every 20 minutes. If this state is not 200, 301, 302, 303 or 304 the user that created this domain on the Ruby on Rails application got notified by the Node.JS application. I deployed this service to heroku.

There are many other ways to monitor your website, this is just an example on heroku that uses a shared database for two applications. If you run your own server, you can do the same by building a Rails application and a cronjob.

Deploying an application to heroku is a very simple solution. You have to install git before you can deploy to heroku. First you have to create some files in a folder. On the command line go into your folder and do the following:

Part 1

//Now you can start to write your Rails Application or use an existing
//Under the following Link you will find an example application that handles
//user login/registration and a user can add some domains to monitor.
https://github.com/wolli2710/pinger_rails

//create a new heroku application

//check the remote branch

//you should see something similar to:

Notice: if something went wront you can remove the remote repository with:

//then you can set the remote repository by using:

//for your railsapp you have to add the following line to your config/application.rb:

//go to the following link and create a database for this app with the postgresql addon:
https://addons.heroku.com/heroku-postgresql
//or simply type the following command in your bash

//Now you are ready to push your application to heroku

//When you pushed your application, and everything is right you should be able to visit your new webapp
//if something went wrong you should check the heroku logs

//if you want to use the example app, don’t forget to change the “_dist” files in the config folder to their original name and pass them your own keys.

Notice: if you have migrations you have to run them before you can see your page:

//Now you should be able to register a user
Notice: For the sample app i disabled the email confirmation mail of devise.


Part 2

For your Nodejs application you have to do the following steps:

//Write your Node.js application or use an existing one:
//Under the following link you will find an application that monitors
//domains of an user and sends a notification mail to the user, if the
//domain can’t be reached by the node.js application
https://github.com/wolli2710/pinger_nodejs

//create a file called package.json for your app dependencies. for example:
If something doesn’t work it could also be a dependency error!! check for compatible versions…

//To declare a process type you have to make a file named “Procfile” in your project
//root folder. In this case, you simply need to execute the Node script using node.
//If your node.js main file is called “server.js” you have to write the following to the “Procfile”

//change into the root of project folder and initialize a git repo

//create a new heroku application

//set the environment to production

//check the remote branch

//check the database url of your rails application

//you should see something like:

//for example:

//set the database url of your nodejs application to the same database
//change into the directory of your nodejs app

//check the DATABASE_URL of your node.js application

//now you can push your application to heroku

//if you want to start your application in development mode you have to do so with NODE_ENV

//then you have to make a file called “keys.js” with the following content

//before you can run the pinger example you have to change the smtp user and password in
//the javascript file “server.js”

//when you finished your application push it to heroku
//with the following command you automatically deploy your application

//if something fails check the logs

Leave a Comment

You must be logged in to post a comment.