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
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.
Very nice, i was just a couple of days ago in talks with someone about exactly this.
ThAnks for the post.
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.
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
[…] more here: Exploring RabbitMQ and PHP – Bradley Holt RSS feed for comments on this post | TrackBack URL | Leave a […]
[…] 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 […]