Exploring RabbitMQ and PHP

I’m exploring the possibility of using RabbitMQ for an upcoming project. RabbitMQ is a free/open source message broker platform. It uses the open Advanced Message Queuing Protocol (AMQP) standard and is written in Erlang using the Open Telecom Platform (OTP). It promises a high level of availability, throughput, scalability, and portability. Since it is built using open standards, it is interoperable with other messaging systems and can be accessed from any platform.

I’ll be using RabbitMQ first from PHP, but I plan on using it to send and receive messages to and from other systems. Following are the steps I used to get RabbitMQ and PHP’s AMQP library setup on my development machine.

First, I installed RabbitMQ using MacPorts:

$ sudo port install rabbitmq-server

Then, I started RabbitMQ:

$ sudo rabbitmq-server -detached

Next, I installed the librabbitmq library using a slight variation of the instructions on PHP’s AMQP Installation page (you may need to install Mercurial first):

$ hg clone http://hg.rabbitmq.com/rabbitmq-c/rev/3c549bb09c16 rabbitmq-c
$ cd rabbitmq-c
$ hg clone http://hg.rabbitmq.com/rabbitmq-codegen/rev/f8b34141e6cb codegen
$ autoreconf -i && ./configure && make && sudo make install

Then, I installed the AMQP extension using PECL:

$ sudo pecl install amqp-beta

To test that everything works, I opened up two interactive PHP shells using php -a. I ran the following code in the first PHP shell:

$exchangeName = 'messages';
$routeKey = 'routeA';
$message = 'Hello, world.';
$connection = new AMQPConnection();
$connection->connect();
$exchange = new AMQPExchange($connection);
$exchange->declare($exchangeName);

I then ran the following code in the second PHP shell:

$exchangeName = 'messages';
$routeKey = 'routeA';
$connection = new AMQPConnection();
$connection->connect();
$queue = new AMQPQueue($connection);
$queue->declare($exchangeName);
$queue->bind($exchangeName, $routeKey);

Back in the first PHP shell:

$exchange->publish($message, $routeKey);

Back in the second PHP shell:

$message = $queue->get();
print_r($message);

Here is the output I got from the print_r statement:

Array
(
    [routing_key] => routeA
    [exchange] => messages
    [delivery_tag] => 1
    [Content-type] => text/plain
    [count] => 0
    [msg] => Hello, world.
)

There are several other options that can be set, and a lot more to learn about RabbitMQ and AMP. Check out the documentation for PHP’s AMQP extension for details about working with AMQP servers from PHP.

4 Comments

  1. Posted July 20, 2011 at 7:17 pm | Permalink

    The documentation was missing a link to its pecl page, but thanks to your blog post it was found and is now fixed. Thanks :) Also, the ‘hg clone’ line was updated to name the dir.

  2. Cristian Datculescu
    Posted July 22, 2011 at 8:58 am | Permalink

    Very nice, i was just a couple of days ago in talks with someone about exactly this.

    ThAnks for the post.

  3. Are Pedersen
    Posted August 1, 2011 at 2:32 am | Permalink

    we have been working with the pecl extension, and we fixed several bugs. But I think there are more bugs in it, so be careful using it in production. Also it lacks much funcionality. You can’t set how many messages the receiver should Max receive, and that is quickly a show stopper. There exists pear amqp libs that works better. don’t have the link here now.

  4. Posted September 22, 2011 at 6:18 pm | Permalink

    Hi Bradley,

    Have you tried these instructions under Lion? or whilst running Zend Server. I’m getting crazy linking errors when I follow the instructions that I can’t find any information on. I know it’s probably going to be a linking issue with my environment but I figured I’d ask anyway :)

    Cheers,
    Jamie

    PHP Warning: PHP Startup: Unable to load dynamic library ‘/usr/local/zend/lib/php_extensions/amqp.so’ – dlopen(/usr/local/zend/lib/php_extensions/amqp.so, 9): Symbol not found: _amqp_empty_bytes
    Referenced from: /usr/local/zend/lib/php_extensions/amqp.so
    Expected in: flat namespace
    in /usr/local/zend/lib/php_extensions/amqp.so in Unknown on line 0

2 Trackbacks

  1. [...] more here: Exploring RabbitMQ and PHP – Bradley Holt RSS feed for comments on this post | TrackBack URL | Leave a [...]

  2. [...] einem neuen Beitrag Bradley Holt befasst sich mit einigen seiner Erforschung der Kombination von RabbitMQ und PHP als eine mögliche Plattform für Messaging zwischen Prozess (oder Anwendungen). Ich bin die [...]