LavinMQ with Ruby

Ruby developers have a number of options for AMQP client libraries. In this tutorial, AMQP::Client is used, an asynchronous client for publishing and consuming of messages.

Before we jump right into writing our code, let’s set up the development environment.

Ruby development environment

  • First make sure that you have Ruby and that it’s available from your command line. You can confirm this by running: ruby --version
  • If you do not have Ruby installed, go to ruby-lang.org or you could even use rbenv
    to manage multiple Ruby versions
  • Open your project directory in your favourite text-editor and create a Gemfile. Add the following gems to the Gemfile: source 'https://rubygems.org' gem 'amqp-client' gem 'dotenv'
  • Install gems with bundler bundle install - running this command should generate a Gemfile.lock file in the directory.
  • If you, however, do not have bundler installed, go ahead and install it with gem install bundler. Alternatively, you can install each gem seperately with gem install gem-name
  • Create a .env file in in the root directory
  • Add CLOUDAMQP_URL="lavinmq_url" to the `.env’ file. Replace lavinmq_url with your correct server url

Publishing messages

require "amqp-client"
require "dotenv/load"

# Grab lavinmq url from .env file
lavinmq_url = ENV['CLOUDAMQP_URL']

# Opens and establishes a connection
connection = AMQP::Client.new(lavinmq_url).connect

# Open a channel
channel = connection.channel
puts "[✅] Connection over channel established"

# Create a queue
queue = channel.queue_declare("hello_world")

def send_to_queue(channel, routing_key, body)
    # Publish function expects: body, exchange, routing_key in that order
    channel.basic_publish(
        body,
        '',
        routing_key
    )
    puts "[📥] Message sent to queue - msg:  #{body}"
end 

# Publish messages
send_to_queue channel, "hello_world", "Hello World - 1"
send_to_queue channel, "hello_world", "Hello World - 2"
send_to_queue channel, "wrong_routing_key", "Hello World - 3"
  
begin
  connection.close
  puts "[❎] Connection closed"
rescue => exception
  puts "Error: #{exception}"
end

Consume messages

require "amqp-client"
require "dotenv/load"

# Grab lavinmq url from .env file
lavinmq_url = ENV['CLOUDAMQP_URL']

# Opens and establishes a connection
connection = AMQP::Client.new(lavinmq_url).connect

# Open a channel
channel = connection.channel
puts "[✅] Connection over channel established"
puts "[❎] Waiting for messages. To exit press CTRL+C "

# Create a queue
queue = channel.queue_declare("hello_world")

counter = 0
# Subscribe to the queue
channel.basic_consume("hello_world") do |msg|
  counter += 1
  # Add logic to handle the message here...
  puts "[📤] Message received [#{counter}]: #{msg.body}"
end

# Close the connection when the script exits
at_exit do 
  client.stop
  puts "[❎] Connection closed"
end

# Keep the consumer running
sleep

Ready to take the next steps? Here are some things you should keep in mind:

Managed LavinMQ instance on CloudAMQP

LavinMQ has been built with performance and ease of use in mind - we've benchmarked a throughput of about 1,000,000 messages/sec. You can try LavinMQ without any installation hassle by creating a free instance on CloudAMQP. Signing up is a breeze.

Help and feedback

We welcome your feedback and are eager to address any questions you may have about this piece or using LavinMQ. Join our Slack channel to connect with us directly. You can also find LavinMQ on GitHub.