CloudAMQP launches a new library for Ruby

For Ruby applications to communicate with RabbitMQ and other applications, CloudAMQP launches a new, smaller, faster and safer AMQP library for Ruby.

Sprung by frustration with current Ruby AMQP libraries, which slow performance, nonsensical errors and unsafe defaults , CloudAMQP now launches a Ruby AMQP client written in pure Ruby with zero dependencies that makes it easier and safer to integrate your Ruby applications with RabbitMQ.

Ruby and RabbitMQ

Ruby is an open-source programming language that has been a world player for decades with developer fans worldwide. Developers have been given plenty of client options for integrating with RabbitMQ over the years whereas Bunny has been the most popular alternative. However, a growing line of tech users, new technology, a wider range of use-cases, and higher requirements on speed and security adds pressure for further technological improvements.

CloudAMQP AMQP::Client

On the 27th of August 2021, CloudAMQP launched version 1.0.0 of the open-source amqp-client. The new client offers plenty of modern adaptations and supports all maintained Ruby versions. Here follows the most significant improvements:

It’s faster!

The CloudAMQP amqp-client is a modern Ruby client, measured to be 4 times faster than other Ruby clients and even faster than the Java client. With only ~1800 lines of code, and without any dependencies, it’s a drastically slimmed-down alternative to other Ruby clients but without having to trade functionality.

Better error handling

AMQP::Client makes it easier to see and understand possible errors that might occur, instead of odd timeout errors connection and channel errors are correctly reported all the time This is an improvement compared to other client libraries where unclarity sometimes leads to excessive and time consuming troubleshooting.

It’s safe by default

With the high-level API, messages sent with AMQP::Client are published as persistent, and are thread safe. This means that messages are stored on disk and will survive broker/server crash or restart (queue must be durable as well). The client also waits for message confirmation from the broker, ensuring that no messages are lost in transit. This can of course be disabled if performance is a priority.

Get started with Ruby and AMQP::Client

Usage

The client has two APIs. A low level API that matches the AMQP protocol very well, it can do everything the protocol allows, but requires some knowledge about the protocol, and doesn't handle reconnects. And, a high-level API that is a bit easier to get started with, and also handles reconnection automatically.

Low level API

require "amqp-client"
# Opens and establishes a connection
conn = AMQP::Client.new("amqp://guest:guest@localhost").connect

# Open a channel
ch = conn.channel

# Create a temporary queue
q = ch.queue_declare

# Publish a message to said queue
ch.basic_publish_confirm "Hello World!", "", q.queue_name, persistent: true

# Poll the queue for a message
msg = ch.basic_get(q.queue_name)

# Print the message's body to STDOUT
puts msg.body

High level API

# Start the client, it will connect and once connected it will reconnect if that connection is lost
# Operation pending when the connection is lost will raise an exception (not timeout)
amqp = AMQP::Client.new("amqp://localhost").start

# Declares a durable queue
myqueue = amqp.queue("myqueue")

# Bind the queue to any exchange, with any binding key
myqueue.bind("amq.topic", "my.events.*")

# The message will be reprocessed if the client loses connection to the broker
# between message arrival and when the message was supposed to be ack'ed.
myqueue.subscribe(prefetch: 20) do |msg|
  process(JSON.parse(msg.body))
  msg.ack
rescue
  msg.reject(requeue: false)
end

# Publish directly to the queue
myqueue.publish({ foo: "bar" }.to_json, content_type: "application/json")

# Publish to any exchange
amqp.publish("my message", "amq.topic", "topic.foo", headers: { foo: 'bar' })
amqp.publish(Zlib.gzip("an event"), "amq.topic", "my.event", content_encoding: 'gzip')

Installation

Get started with amqp-client by adding this line to your application's Gemfile:

gem 'amqp-client'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install amqp-client

We at CloudAMQP always strive for meeting customer and community demands and we hope that you like our contribution. Further reading and github links is available here:

  1. GitHub repository: https://github.com/cloudamqp/amqp-client.rb
  2. API reference documentation: https://cloudamqp.github.io/amqp-client.rb/

And, if you have any questions or comments about this new client, send us an email at contact@cloudamqp.com

Until next time, CloudAMQP team

CloudAMQP - industry leading RabbitMQ as a service

Start your managed cluster today. CloudAMQP is 100% free to try.

13,000+ users including these smart companies