ComLaude / laravel-amqp by david.krizanic

AMQP wrapper for Laravel and Lumen to publish and consume messages
1,738
0
7
Package Data
Maintainer Username: david.krizanic
Maintainer Contact: david.krizanic@comlaude.com (David Krizanic)
Package Create Date: 2020-09-09
Package Last Update: 2021-05-12
Language: PHP
License: MIT
Last Refreshed: 2021-11-20 15:00:10
Package Statistics
Total Downloads: 1,738
Monthly Downloads: 160
Daily Downloads: 2
Total Stars: 0
Total Watchers: 7
Total Forks: 0
Total Open Issues: 0

ComLaude/laravel-amqp

Simple PhpAmqpLib wrapper for interaction with RabbitMQ

Installation

Composer

Add the following to your require part within the composer.json:

"comlaude/laravel-amqp": "1.*"
$ php composer update

or

$ php composer require comlaude/laravel-amqp

Integration

Lumen

Create a config folder in the root directory of your Lumen application and copy the content from vendor/comlaude/laravel-amqp/config/amqp.php to config/amqp.php.

Adjust the properties to your needs.

return [

    'use' => 'production',

    'properties' => [

        'production' => [
            'host'                => 'localhost',
            'port'                => 5672,
            'username'            => 'guest',
            'password'            => 'guest',
            'vhost'               => '/',
            'exchange'            => 'amq.topic',
            'exchange_type'       => 'topic',
            'consumer_tag'        => 'consumer',
            'ssl_options'         => [], // See https://secure.php.net/manual/en/context.ssl.php
            'connect_options'     => [], // See https://github.com/php-amqplib/php-amqplib/blob/master/PhpAmqpLib/Connection/AMQPSSLConnection.php
            'queue_properties'    => ['x-ha-policy' => ['S', 'all']],
            'exchange_properties' => [],
            'timeout'             => 0
        ],

    ],

];

Register the Lumen Service Provider in bootstrap/app.php:

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
*/

//...

$app->configure('amqp');
$app->register(ComLaude\Amqp\LumenServiceProvider::class);

//...

Add Facade Support for Lumen 5.2+

//...
$app->withFacades(true, [
    'ComLaude\Amqp\Facades\Amqp' => 'Amqp',
]);
//...

Laravel

Open config/app.php and add the service provider and alias:

'ComLaude\Amqp\AmqpServiceProvider',
'Amqp' => 'ComLaude\Amqp\Facades\Amqp',

Publishing a message

Push message with routing key

    Amqp::publish('routing-key', 'message');

Push message with routing key and custom queue

    Amqp::publish('routing-key', 'message' , ['queue' => 'queue-name']);

Push message with routing key and overwrite properties

    Amqp::publish('routing-key', 'message' , ['exchange' => 'amq.direct']);

Consuming messages

Consume messages forever

Amqp::consume(function ($message) {
    		
   var_dump($message->body);

   Amqp::acknowledge($message);
        
});

Consume messages, with custom settings

Amqp::consume(function ($message) {
    		
   var_dump($message->body);

   Amqp::acknowledge($message);
      
}, [
	'timeout' => 2,
    'vhost'   => 'vhost3',
    'queue'   => 'queue-name',
    'persistent' => true // required if you want to listen forever
]);

Fanout example

Publishing a message

\Amqp::publish('', 'message' , [
    'exchange_type' => 'fanout',
    'exchange' => 'amq.fanout',
]);

Credits

  • Some concepts were used from https://github.com/bschmitt/laravel-amqp

License

This package is open-sourced software licensed under the MIT license