About amqp gem

amqp gem is a widely used, feature-rich asynchronous Ruby AMQP client built on top of EventMachine. This library works with Ruby 1.8.7, Ruby 1.9.2, JRuby, REE and Rubinius, and is licensed under the Ruby License. Versions 0.8.0 and later of amqp gem implement AMQP 0.9.1.

What is AMQP?

AMQP is an open standard for messaging middleware that emphasizes interoperability between different technologies (for example, Java, .NET, Ruby, Python, C and so on).

Key features of AMQP are very flexible yet simple routing and binary protocol efficiency.

What is amqp gem good for?

One can use amqp gem to make Ruby applications interoperate with other applications (both Ruby and not). Complexity and size may vary from simple work queues to complex multi-stage data processing workflows that involve dozens or hundreds of applications built with all kinds of technologies.

Specific examples:

Getting started with amqp gem

Install RabbitMQ

Please refer to RabbitMQ installation guide Note that for Ubuntu and Debian we strongly advice that you use RabbitMQ apt repository that has recent versions of RabbitMQ. Ubuntu (even 10.10) and Debian both ship with old RabbitMQ version, that only supports AMQP protocol 0.8. amqp gem 0.8.0 and later will not work with RabbitMQ versions before 2.0.

Install the gem

gem install amqp

“Hello, World” example

#!/usr/bin/env ruby
# encoding: utf-8

require "rubygems"
require 'amqp'

EventMachine.run do
  AMQP.connect(:host => 'localhost') do |connection|
    puts "Connected to AMQP broker"

    channel  = AMQP::Channel.new(connection)
    queue    = channel.queue("amqpgem.examples.hello_world")
    exchange = channel.default_exchange

    queue. do |payload|
      puts "Received a message: #{payload}. Disconnecting..."

      connection.close {
        EM.stop { exit }
      }
    end

    exchange.publish "Hello, world!", :routing_key => queue.name
  end  
end

(see as a Gist)

How to use AMQP gem with Ruby on Rails, Merb, Sinatra and other web frameworks

To use AMQP gem from web applications, you would need to have EventMachine reactor running. If you use Thin or Goliath, you are all set: those two servers use EventMachine under the hood.

With other web servers, you need to start EventMachine reactor in a separate thread like this:

Thread.new { EM.run }

Otherwise EventMachine will block current thread.

Then connect to AMQP broker:

amqp_connection = AMQP.connect(:host => "localhost", :user => "guest", :pass => "guest", :vhost => "/")

In a Ruby on Rails app, probably the best place for this code is initializer (like config/initializers/amqp.rb). For Merb apps, it is config/init.rb. For Sinatra and pure Rack applications, place it next to other configuration code.

If you want to integrate AMQP with Thin, Goliath or some other EventMachine-based software which already runs an event loop, you might want to use following code:

EM.next_tick { AMQP.connect(...) }

So in case EventMachine reactor isn’t running yet on server/application boot, connection won’t fail but instead wait for reactor to start.

Same separate thread technique can be used to make EventMachine play nicely with other libraries that would block current thread (like File::Tail).

Using amqp gem in your app/library using Bundler

With Bundler, add this line to your Gemfile:

gem "amqp"

If you want to use edge version (usually there is no need to):

gem "amqp", :git => "git://github.com/ruby-amqp/amqp.git", :branch => "master"

Examples

You can find many examples (both real-world cases and simple demonstrations) under examples directory in the repository.

How does amqp gem relate to amq-client gem, amq-protocol and libraries like bunny?

See this page about AMQP gems family

How can I learn more?

AMQP resources

Messaging and distributed systems resources

Community

License

AMQP gem is licensed under the “Ruby License:”http://www.ruby-lang.org/en/LICENSE.txt.

Credits and copyright information

Currently maintained by ruby-amqp group members Special thanks to Dmitriy Samovskiy, Ben Hood and Tony Garnock-Jones.

FAQ

So, does amqp gem only work with RabbitMQ?

This library was tested primarily with RabbitMQ, although it should be compatible with any server implementing the AMQP 0.9.1 spec. For AMQP 0.8.0 brokers, use version 0.7.

Links