CloudAMQP with Ruby Getting started

Client libraries Many options

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

  • Bunny is the most popular client
  • March Hare is an idiomatic, fast and well-maintained (J)Ruby DSL on top of the RabbitMQ Java client
  • AMQP EventMachine-based RabbitMQ client. Only use if you are already using EventMachine

Code example Publish and subscribe synchronously

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

We begin by adding the environment variable CLOUDAMQP_URL to our .env file. We can find our AMQP URL on CloudAMQP console page.

➜ vim .env
CLOUDAMQP_URL="amqp://user:password@host/user"
~

Then, we add these gems to our Gemfile if they are not yet added.

➜ vim Gemfile
source 'https://rubygems.org'
ruby '2.5.1'
gem 'bunny'
gem 'dotenv'
~

Further, we create a script and name it publish.rb. In the publish.rb script we will write down a simple example on how to publish a message.

require "bunny"
require "dotenv/load"

connection = Bunny.new ENV['CLOUDAMQP_URL']
connection.start # Start a communication session with the amqp server
channel = connection.create_channel # Declare a channel
queue = channel.queue("bunny_queue") # Declare a queue

# Declare a default direct exchange which is bound to all queues
exchange = channel.exchange("")

# Publish a message to the exchange which then gets routed to the queue
exchange.publish("Hello everybody!", :key => queue.name)

connection.close # Finally, close the connection

Then, we create another script and name it subscribe.rb. In the subscribe.rb script we will write down a simple example on how to consume a message.

require "bunny"
require 'dotenv/load'

connection = Bunny.new ENV['CLOUDAMQP_URL']
connection.start
channel = connection.create_channel
queue = channel.queue("bunny_queue")

begin
  puts ' [*] Waiting for messages. To exit press CTRL+C'
  queue.subscribe(block: true) do |_delivery_info, _properties, body|
    puts " [x] Consumed the message: []"
  end
rescue Interrupt => _
  connection.close
  exit(0)
end

Finally, in each terminal window, we run the scripts subscribe.rb and publish.rb respectively.

➜ ruby publish.rb
➜
➜ ruby subscribe.rb
> [*] Waiting for messages. To exit press CTRL+C
> [x] Received Hello everybody!

Code example using Sneakers

The following example will use sneakers to consume a message from RabbitMQ

#sneakers_app.rb

require 'sneakers' # don't forget to put gem "sneakers" in your Gemfile
require 'sneakers/runner'

class Processor
  include Sneakers::Worker
  from_queue :default_queue
  def work(msg)
    puts "Msg received: " + msg
  end
end

opts = {
  :amqp => 'CLOUDAMQP_URL', # Replace the string with your AMQP url that you can find on CloudAMQP console page
  :vhost => 'username', # Replace with the vhost/username that you can find on CloudAMQP console page
  :exchange => 'sneakers',
  :exchange_type => :direct
}

Sneakers.configure(opts)
r = Sneakers::Runner.new([Processor])
r.run

Run your application, $ ruby sneakers_app.rb .

If you go to your RabbitMQ admin console -> queues, you will see that a new queue named default_queue is created. Push a couple of messages by entering a text into the payload under Publish message, and press Publish message. You should now see some output in your terminal, telling you that the message is received and that it is ready to be handled.

Code example Build realtime apps!

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.

The application can also be seen live at amqp-sse.herokuapp.com.

Video

Here's a video recording showing how to setup a CloudAMQP instance and connect to it using Ruby and the Bunny library:

Further reading