This add-on is operated by 84codes AB

RabbitMQ as a Service


Last Updated: 11 June 2015

Table of Contents

CloudAMQP is an add-on providing RabbitMQ as a service. RabbitMQ is a high performance message broker, built in Erlang, which implements the AMQP protocol.

Messaging is the easiest and most efficient way to decouple, distribute and scale applications.

All AMQP client libraries work with CloudAMQP and there’s AMQP client libraries for almost every platform out there, including Ruby, Node.js, Java, Python, Clojure and Erlang.

Installing the add-on

CloudAMQP can be installed to a Heroku application via the CLI:

A list of all plans available can be found here.

Number of nodes can be specified for instances on plan Roaring Rabbit and larger. The default setting for those instancs are two nodes - it will give you two mirrored nodes. If you choose one node it will give you twice the performance and three nodes will half the performace but give you pause minitory on partitions. Number of nodes can be specified when you create your instance with the parameter --nodes.

$ heroku addons:create cloudamqp
-----> Adding cloudamqp to sharp-mountain-4005... done, v18 (free)

Example for Power Panda with one node:

$ heroku addons:create cloudamqp:panda --nodes=1

Once CloudAMQP has been added a CLOUDAMQP_URL setting will be available in the app configuration and will contain the canonical URL used to access the RabbitMQ cluster. This can be confirmed using the heroku config command.

$ heroku config | grep CLOUDAMQP_URL
CLOUDAMQP_URL    => amqp://

After installing CloudAMQP the application should be configured to fully integrate with the add-on.

Local workstation setup

RabbitMQ is easy to install locally for development.

If you have… Install with…
Mac OS X brew install rabbitmq
Windows The installer
Ubuntu Linux
sudo echo "deb testing main" >> /etc/apt/sources.list
sudo apt-key add rabbitmq-signing-key-public.asc
sudo apt-get update
sudo apt-get install rabbitmq-server
Other See RabbitMQ’s installation page

Use with Ruby

Ruby developers has a number of options for AMQP client libraries:

  • Bunny the AMQP client, synchronous and very well maintained.
  • AMQP an evented AMQP client, good in combination with an evented web server like Thin.
  • March Hare AMQP client for JRuby, uses the Java RabbitMQ library underneath

The following example will use the synchronous client Bunny and publish a message and then consume it.

require "bunny" # don't forget to put gem "bunny" in your Gemfile

b.start # start a communication session with the amqp server

q = b.queue 'test1' # declare a queue

# publish a message to the queue
q.publish 'Hello, everybody!'

delivery_properties, headers, payload = q.pop # retrieve one message from the queue

puts "This is the message: " + payload + "\n\n"

b.stop # close the connection

Here’s another example which uses the evented AMQP library, in combination with Sinatra and Server Sent Events. It shows how to build a simple real time application, using CloudAMQP as the backbone.

Find the Ruby Sinatra SSE sample on GitHub.
Source code or Deploy

The application can also be seen live at

Further reading

Use with Node.js

For Node.js we recommend amqplib. We highly advice against the former popular library node-amqp, it’s not very well maintained has some some serious bugs.

Add amqplib as a dependency in your package.json file.

"dependencies": {
  "amqplib": "*"

The following code snippet show how to connect and both publish messages and how to subscribe to a queue:

var q = 'tasks';

var url = process.env.CLOUDAMQP_URL || "amqp://localhost";
var open = require('amqplib').connect(url);

// Consumer
open.then(function(conn) {
  var ok = conn.createChannel();
  ok = ok.then(function(ch) {
    ch.consume(q, function(msg) {
      if (msg !== null) {
  return ok;
}).then(null, console.warn);

// Publisher
open.then(function(conn) {
  var ok = conn.createChannel();
  ok = ok.then(function(ch) {
    ch.sendToQueue(q, new Buffer('something to do'));
  return ok;
}).then(null, console.warn);

Use with Clojure

Langohr is a full featured and well maintained Clojure wrapper for Java’s AMQP library.

To use it put [com.novemberain/langohr "2.9.0"] in your project.clj file and run lein deps.

The following code snippet show how to both publish and to consume a message via CloudAMQP:

(ns clojure-amqp-example.core
  (:require [langohr.core :as rmq]
            [ :as lch]
            [langohr.queue :as lq]
            [langohr.consumers :as lc]
            [langohr.basic :as lb]))

(defn -main [& args]
  (let [url (System/getenv "CLOUDAMQP_URL" "amqp://localhost")
        conn (rmq/connect {:uri url})
        ch (lch/open conn)
        qname "langohr.examples.hello-world"]
    (println (format "[main] Connected. Channel id: %d" (.getChannelNumber ch)))
    (lq/declare ch qname :exclusive false :auto-delete true)
    (start-consumer ch qname)
    (println "[main] Publishing...")
    (lb/publish ch default-exchange-name qname "Hello!" :content-type "text/plain")
    (Thread/sleep 2000)
    (println "[main] Disconnecting...")
    (rmq/close ch)
    (rmq/close conn)))

The full Clojure sample app is available on GitHub.
Source code or Deploy

Use with Python

The recommended library for Python to access RabbitMQ servers is Pika.

Put pika==0.9.14 in your requirement.txt file.

The following code connects to CloudAMQP, declares a queues, publish a message to it, setups a subscription and print messages coming to the queue.

import pika, os, urlparse

# Parse CLODUAMQP_URL (fallback to localhost)
url_str = os.environ.get('CLOUDAMQP_URL', 'amqp://guest:guest@localhost//')
url = urlparse.urlparse(url_str)
params = pika.ConnectionParameters(host=url.hostname, virtual_host=url.path[1:],
    credentials=pika.PlainCredentials(url.username, url.password))

connection = pika.BlockingConnection(params) # Connect to CloudAMQP
channel = # start a channel
channel.queue_declare(queue='hello') # Declare a queue
# send a message
channel.basic_publish(exchange='', routing_key='hello', body='Hello CloudAMQP!')
print " [x] Sent 'Hello World!'"

# create a function which is called on incoming messages
def callback(ch, method, properties, body):
  print " [x] Received %r" % (body)

# set up subscription on the queue

channel.start_consuming() # start consuming (blocks)


The full code can be seen at


Celery is a great task queue library for Python, and the AMQP backend works perfectly with CloudAMQP. But remember to tweak the BROKER_POOL_LIMIT if you’re using the free plan. Set it to 1 and you should be good. If you have connection problems, try reduce the concurrency of both your web workers and in the celery worker.

Use with Java

RabbitMQ has developed an excellent Java AMQP library.

Begin to add the AMQP library as an dependency in your pom.xml file:


Then may a simple publish and subscribe look like this:

public static void main(String[] args) throws Exception {
  String uri = System.getenv("CLOUDAMQP_URL");
  if (uri == null) uri = "amqp://guest:guest@localhost";

  ConnectionFactory factory = new ConnectionFactory();
  Connection connection = factory.newConnection();
  Channel channel = connection.createChannel();

  channel.queueDeclare("hello", false, false, false, null);
  String message = "Hello CloudAMQP!";
  channel.basicPublish("", "hello", null, message.getBytes());
  System.out.println(" [x] Sent '" + message + "'");

  QueueingConsumer consumer = new QueueingConsumer(channel);
  channel.basicConsume("hello", true, consumer);

  while (true) {
    QueueingConsumer.Delivery delivery = consumer.nextDelivery();
    String message = new String(delivery.getBody());
    System.out.println(" [x] Received '" + message + "'");

The full Java sample app is available on GitHub.
Source code or Deploy

Java AMQP resources

Use with PHP

For PHP the recommend library is php-amqplib.

Your composer.json should define both php-amqplib and ext-bcmath:

  "require": {
    "videlalvaro/php-amqplib": "2.2.*",
    "ext-bcmath": "*"

Here’s an example which publishes to a queue and then retrieves it.

define('AMQP_DEBUG', true);
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;
$url = parse_url(getenv('CLOUDAMQP_URL'));
$conn = new AMQPConnection($url['host'], 5672, $url['user'], $url['pass'], substr($url['path'], 1));
$ch = $conn->channel();

$exchange = '';
$queue = 'basic_get_queue';
$ch->queue_declare($queue, false, true, false, false);
$ch->exchange_declare($exchange, 'direct', true, true, false);
$ch->queue_bind($queue, $exchange);

$msg_body = 'the body';
$msg = new AMQPMessage($msg_body, array('content_type' => 'text/plain', 'delivery_mode' => 2));
$ch->basic_publish($msg, $exchange);

$retrived_msg = $ch->basic_get($queue);


An example PHP app can be found on GitHub.
Source code or Deploy

For many more involved examples see php-amqplib’s demo directory


The CloudAMQP dashboard allows you to show the current message rate, which queues and exchanges you have, and the bindings between them. You can also queue and pop messages manually, among other things.

CloudAMQP Dashboard

The dashboard can be accessed via the CLI:

$ heroku addons:open cloudamqp
Opening cloudamqp for sharp-mountain-4005…

or by visiting the Heroku apps web interface and selecting the application in question. Select CloudAMQP from the Add-ons menu.

Migrating between plans

Plan migrations are easy and instant. Use the heroku addons:upgrade command to migrate to a new plan.

$ heroku addons:upgrade cloudamqp:rabbit
-----> Upgrading cloudamqp:rabbit to sharp-mountain-4005... done, v18 ($600/mo)
       Your plan has been updated to: cloudamqp:rabbit

Removing the add-on

CloudAMQP can be removed via the CLI.

This will destroy all associated data and cannot be undone!

$ heroku addons:destroy cloudamqp
-----> Removing cloudamqp from sharp-mountain-4005... done, v20 (free)

Error codes

We log errors to your heroku log, below we explain the different codes.

410 - Transfer limit reached

You’ve reached your monthly transfer quota. Upgrade to a larger plan or wait until the next calendar month.

210 - Transfer in compliance

You’ve either upgraded your account (got a higher transfer limit) or it’s a new calendar month and your quota has been reseted.

420 - Connection limit reached

You’re using all your connection slots so new connections will be blocked. Either lower your connection count or upgrade to larger plan to accommodate more connections.

220 - Connections in compliance

You can now open more connections again because you’re not using all connection slots.

431 - Max channels per connection

One of your connections was closed because you’d open to many channels on it. This is often due to a bug, so check your code and make sure that you close unused channels.

432 - Max consumers per connection

One of your connections was closed because it had opened more than 12000 consumers. This is often due to a bug, so make sure that you close unused consumers.


All CloudAMQP support and runtime issues should be logged with Heroku Support at Any non-support related issues or product feedback is welcome at or via Twitter @CloudAMQP. For our plans Power Panda and larger, we provide 24/7 critical support with a 15-minutes maximum initial respons time. For the plans Heavy Hippo and Awesome Ape can we provide direct phone numbers.