AMQP is a painless Way to Use RabbitMQ with Laravel

AMQP is a painless Way to Use RabbitMQ with Laravel

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:

app('amqp')->publish('Message to direct exchange', 'routing-key', [
    'exchange' => [
        'type'    => 'direct',
        'name'    => 'direct.exchange',
    ],
]);

And here’s how you might consume a message:

use Anik\Amqp\ConsumableMessage;

// AmqpManager::consume($consumerHandler, $bindingKey, $config);
app('amqp')->consume(function (ConsumableMessage $message) {
    echo $message->getStream() . PHP_EOL;
    $message->getDeliveryInfo()->acknowledge();
}, 'routing-key', [
    'connection' => 'my-connection-name',
    'exchange'   => [
        'type'    => 'direct',
        'name'    => 'direct.exchange',
    ],
    'queue' => [
        'name'         => 'direct.exchange.queue',
        'declare'      => true,
        'exclusive'    => false,
    ],
    'qos' => [
        'enabled'            => true,
        'qos_prefetch_count' => 5,
    ],
]);

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: News


Newsletter

Join the weekly newsletter and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Web Developer
Remote
Livesystems dooh AG
Senior Backend Engineer
Remote
64 Robots
Senior Laravel Developer
Atlanta, GA
Helium LLC
Full-Stack Developer (JS - Vue.js, PHP - Laravel)
US Remote / Telecommute
TTEC Digital
Backend Engineer
Brooklyn, NY
Stationhead
Back-End Developer
Orlando, FL
Christ for all Nations
Senior Software Developer
Nashville, TN
Bernard Health