Ruby developers have several options for AMQP client libraries:
AMQP::Client is CloudAMQP's official Ruby client. It provides a high-level API with automatic connection management, built-in message encoding/decoding, RPC patterns, and excellent performance. It's the recommended choice for new Ruby projects.
$ bundle init && bundle add amqp-client
Create a file named
publish.rb:
require "amqp-client"
# Connect to CloudAMQP
client = AMQP::Client.new(ENV.fetch("CLOUDAMQP_URL")).start
# Publish a message
client.queue("my_queue").publish("Hello World")
puts "Message published"
Create a file named
subscribe.rb:
require "amqp-client"
# Connect to CloudAMQP
client = AMQP::Client.new(ENV.fetch("CLOUDAMQP_URL")).start
# Consume messages
puts "[*] Waiting for messages. To exit press CTRL+C"
client.queue("my_queue").subscribe do |msg|
puts "[x] Received: #{msg.body}"
end
sleep
In separate terminal windows, run:
# Terminal 1
$ export CLOUDAMQP_URL=amqps://user:pass@change-me.rmq.cloudamqp.com/user
$ bundle exec ruby subscribe.rb
[*] Waiting for messages. To exit press CTRL+C
# Terminal 2
$ export CLOUDAMQP_URL=amqps://user:pass@change-me.rmq.cloudamqp.com/user
$ bundle exec ruby publish.rb
Message published
Expected output in Terminal 1:
[x] Received: Hello World
The high-level API includes built-in support for:
See the GitHub repository for complete documentation. For advanced use cases, a low-level API is also available.
Bunny is a popular synchronous AMQP client with a large community. It's a solid choice if you prefer explicit control over connections and channels.
$ bundle init && bundle add logger bunny
Create a file named
publish.rb:
require "bunny"
connection = Bunny.new(ENV.fetch("CLOUDAMQP_URL"), verify_peer: true)
connection.start
channel = connection.create_channel
queue = channel.queue("bunny_queue")
# Publish a message
channel.default_exchange.publish("Hello everybody!", routing_key: queue.name)
connection.close
Create a file named
subscribe.rb:
require "bunny"
connection = Bunny.new(ENV.fetch("CLOUDAMQP_URL"), verify_peer: true)
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] Received: #{body}"
end
rescue Interrupt
connection.close
exit(0)
end
In separate terminal windows, run:
# Terminal 1
$ export CLOUDAMQP_URL=amqps://user:pass@change-me.rmq.cloudamqp.com/user
$ bundle exec ruby subscribe.rb
[*] Waiting for messages. To exit press CTRL+C
# Terminal 2
$ export CLOUDAMQP_URL=amqps://user:pass@change-me.rmq.cloudamqp.com/user
$ bundle exec ruby publish.rb
Expected output in Terminal 1:
[x] Received: Hello everybody!
For more information, visit rubybunny.info.
March Hare is an idiomatic JRuby DSL built on top of the RabbitMQ Java client. It's the recommended choice for JRuby applications.
Make sure you have JRuby 9.x or later installed:
$ rvm install jruby
$ bundle init && bundle add logger march_hare
Create a file named
publish.rb:
require "march_hare"
connection = MarchHare.connect(uri: ENV.fetch("CLOUDAMQP_URL"))
channel = connection.create_channel
queue = channel.queue("hare_queue")
# Publish a message
channel.default_exchange.publish("Hello Hare!", routing_key: queue.name)
connection.close
Create a file named
subscribe.rb:
require "march_hare"
connection = MarchHare.connect(uri: ENV.fetch("CLOUDAMQP_URL"))
channel = connection.create_channel
queue = channel.queue("hare_queue")
begin
puts "[*] Waiting for messages. To exit press CTRL+C"
queue.subscribe do |metadata, payload|
puts "[x] Received: #{payload}"
end
sleep
rescue Interrupt
connection.close
exit(0)
end
In separate terminal windows, run:
# Terminal 1
$ export CLOUDAMQP_URL=amqps://user:pass@change-me.rmq.cloudamqp.com/user
$ bundle exec ruby subscribe.rb
[*] Waiting for messages. To exit press CTRL+C
# Terminal 2
$ export CLOUDAMQP_URL=amqps://user:pass@change-me.rmq.cloudamqp.com/user
$ bundle exec ruby publish.rb
Expected output in Terminal 1:
[x] Received: Hello Hare!
For more information, visit rubymarchhare.info.
Kicks (previously named Sneakers) is a high-performance background job processing framework built on RabbitMQ. It's ideal for building worker-based architectures.
$ bundle init && bundle add kicks
Create a file named
kicks_app.rb:
require "sneakers"
require "sneakers/runner"
class MessageProcessor
include Sneakers::Worker
from_queue "my_queue"
def work(msg)
puts "Processing: #{msg}"
ack!
end
end
Sneakers.configure(
amqp: ENV.fetch("CLOUDAMQP_URL"),
exchange: "sneakers",
exchange_type: :direct
)
Sneakers::Runner.new([MessageProcessor]).run
$ export CLOUDAMQP_URL=amqps://user:pass@change-me.rmq.cloudamqp.com/user
$ bundle exec ruby kicks_app.rb
The worker will create a queue named
my_queue
and wait for messages. You can publish messages through the RabbitMQ management console
or using any AMQP client.
Watch this video tutorial showing how to set up a CloudAMQP instance and connect using Ruby: