Hosting on Heroku

Heroku is a popular host for many Ruby on Rails developers. Using it with Apiotics requires making a few simple changes to the configuration of your application, and also requires making a few simple changes on Heroku itself. We will walk through them here.

Apiotics Initializer Changes

To use Heroku as a host, we need to use a strategy for communications between your web application and the Apiotics local communications server that is Heroku friendly. The default communications strategy, using a local TCP port, is not something that Heroku supports.

To deal with this, we will use Redis as a message queue to send messages back and forth between your web application and the Apiotics comms server.

To do that, we need to make the following change to the app/config/initializers/apiotics.rb file:

config.redis_comms_connection = true
config.heroku = true

Gemfile Changes

In order to use Redis as a message queue, we also need to add the Redis gem to our Gemfile. Add (or uncomment) the following line in your application's Gemfile:

gem 'redis', '~> 3.0'

Note: as of this writing, there are incompatibilities with version 4.0 and above of the Redis gem, so be sure to include the version limitation in the above line.

Add a Procfile

Heroku uses web dynos to run your web application and worker dynos to run other functionality you may need. The Apiotics local comms server is an example of this kind of other functionality. In order for Heroku to understand what to run when it runs a worker dyno, you use a Procfile.

This Procfile lives at the top level directory of your web application and looks like the following:

web: `bundle exec rails server -p $PORT`
worker: `bundle exec rake comms:restart`

Add Redis to your Heroku App

On the pages for your application on, there is a section called "resources". On that page you can (and should) add Heroku Redis to your app.

Create a Redis Initializer

In app/config/initializers, create a redis.rb file with the following in it:

$redis = ENV["REDIS_URL"])

Deploy to Heroku

Deploy your application in the usual way to Heroku by running:

git push heroku master

And don't forget to migrate your database if need be:

heroku run rake db:migrate

Scale Your Workers

Finally, make sure that an instance of your comms server is running on Heroku:

heroku ps:scale worker=1

And with that you should be up and running on Heroku with Apiotics!

Mac Dougherty