AMQP is a painless Way to Use RabbitMQ with Laravel


September 12th, 2019


The anik/amqp package by Syed Sirajul Islam Anik is a php-amqplib wrapper that eases the consumption of RabbitMQ.

You can use this package with Laravel, Lumen, and Laravel Zero. At the time of writing, you will need to configure the service provider manually (a pull request was submitted for package discovery).

We recently wrote about a Laravel Queue Driver for RabbitMQ package, which is a queue driver that enables you to use RabbitMQ with the Laravel queue API. You can think of anik/amqp more as a general package with conveniences on top of php-amqplib, and the RabbitMQ queue driver as a drop-in driver for Laravel queues.

From the package’s readme, here’s how you might publish a message:

1app('amqp')->publish('Message to direct exchange', 'routing-key', [
2 'exchange' => [
3 'type' => 'direct',
4 'name' => '',
5 ],

And here’s how you might consume a message:

1use Anik\Amqp\ConsumableMessage;
3// AmqpManager::consume($consumerHandler, $bindingKey, $config);
4app('amqp')->consume(function (ConsumableMessage $message) {
5 echo $message->getStream() . PHP_EOL;
6 $message->getDeliveryInfo()->acknowledge();
7}, 'routing-key', [
8 'connection' => 'my-connection-name',
9 'exchange' => [
10 'type' => 'direct',
11 'name' => '',
12 ],
13 'queue' => [
14 'name' => '',
15 'declare' => true,
16 'exclusive' => false,
17 ],
18 'qos' => [
19 'enabled' => true,
20 'qos_prefetch_count' => 5,
21 ],

For the documentation (and for a nice overview of RabbitMQ) check out the package author’s post: RabbitMQ for PHP Developers. You can learn more about this package, get full installation instructions, and view the source code on GitHub at ssi-anik/amqp.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.