mmanazar / laravel-amqp by mmanazar
forked from bschmitt/laravel-amqp

AMQP wrapper for Laravel and Lumen to publish and consume messages in bulk
13
0
1
Package Data
Maintainer Username: mmanazar
Maintainer Contact: mmanazar@gmail.com (Muhammad Ben Manazar)
Package Create Date: 2017-03-29
Package Last Update: 2017-03-30
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2021-11-24 03:17:50
Package Statistics
Total Downloads: 13
Monthly Downloads: 1
Daily Downloads: 0
Total Stars: 0
Total Watchers: 1
Total Forks: 0
Total Open Issues: 0

bschmitt/laravel-amqp

AMQP wrapper for Laravel and Lumen to publish and consume messages especially from RabbitMQ

Build Status Latest Stable Version License

Features

  • Advanced queue configuration
  • Add message to queues easily
  • Listen queues with useful options

Installation

Composer

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

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

or

$ php composer require bschmitt/laravel-amqp

Integration

Lumen and Laravel Both

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

Adjust the properties to your needs in here or .env file

return [

    'use' => 'production',

    'properties' => [

        'production' => [
            'host'                => '',
            'port'                => '',
            'username'            => '',
            'password'            => '',
            'vhost'               => '/',
            'connect_options'     => [],
            'ssl_options'         => [],

            'exchange'              => 'amq.topic',
            'exchange_type'         => 'topic',
            'exchange_passive'      => false,
            'exchange_durable'      => true,
            'exchange_auto_delete'  => false,
            'exchange_internal'     => false,
            'exchange_nowait'       => false,
            'exchange_properties'   => [],

            'queue_force_declare'   => false,
            'queue_passive'         => false,
            'queue_durable'         => true,
            'queue_exclusive'       => false,
            'queue_auto_delete'     => false,
            'queue_nowait'          => false,
            'queue_properties'      => ['x-ha-policy' => ['S', 'all']],

            'consumer_tag'      => '',
            'consumer_no_local'   => false,
            'consumer_no_ack'     => true,
            'consumer_exclusive'  => false,
            'consumer_nowait'     => false,
            'timeout'             => 0,
            'persistent'          => true,
        ],

    ],

];

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

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

//...

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

//...

Add Facade Support for Lumen 5.2+

//...
$app->withFacades();
class_alias(\Illuminate\Support\Facades\App::class, 'App');
//...

Laravel

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

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

Publishing a message

Push message with routing key

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

Push message with routing key and create 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']);

Push bulk messages with routing key

    Amqp::publish('routing-key', ['message1', 'message2', ......], a);

Consuming messages

Consume messages, acknowledge and stop when no message is left

Amqp::consume('queue-name', function ($message, $resolver) {
    		
   var_dump($message->body);

   $resolver->acknowledge($message);

   $resolver->stopWhenProcessed();
        
});

Consume messages forever

Amqp::consume('queue-name', function ($message, $resolver) {
    		
   var_dump($message->body);

   $resolver->acknowledge($message);
        
});

Consume messages, with custom settings

Amqp::consume('queue-name', function ($message, $resolver) {
    		
   var_dump($message->body);

   $resolver->acknowledge($message);
      
}, [
	'timeout' => 2,
	'vhost'   => 'vhost3'
]);

Fanout example

Publishing a message

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

Consuming messages

\Amqp::consume('', function ($message, $resolver) {
    var_dump($message->body);
    $resolver->acknowledge($message);
}, [
    'exchange' => 'amq.fanout',
    'exchange_type' => 'fanout',
    'queue_force_declare' => true,
    'queue_exclusive' => true,
    'persistent' => true// required if you want to listen forever
]);

Credits

  • Some concepts were used from https://github.com/mookofe/tail

License

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