2017 © Pedro Peláez
 

library larabbit

AMQP wrapper for Laravel and Lumen to publish and consume messages

image

filld/larabbit

AMQP wrapper for Laravel and Lumen to publish and consume messages

  • Friday, June 29, 2018
  • by russianryebread
  • Repository
  • 10 Watchers
  • 0 Stars
  • 541 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 33 Forks
  • 0 Open issues
  • 11 Versions
  • 28 % Grown

The README.md

filld/larabbit

AMQP wrapper for Laravel and Lumen to publish and consume messages especially from RabbitMQ, (*1)

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:, (*2)

"filld/larabbit": "2.*"

```batch $ php composer update, (*3)


or

$ php composer require filld/larabbit, (*4)


## Integration ### Environment Variables The following Env vars need to be set in your `.env` file: RABBITMQ_HOST RABBITMQ_PORT RABBITMQ_USERNAME RABBITMQ_PASSWORD RABBITMQ_VHOST # Optional SSL: RABBITMQ_SSL_CERT_PATH ### Lumen Create a **config** folder in the root directory of your Lumen application and copy the content from **vendor/filld/larabbit/config/amqp.php** to **config/amqp.php**. Adjust the properties to your needs. ```php return [ 'use' => 'production', 'properties' => [ 'production' => [ 'host' => env('RABBITMQ_HOST', 'localhost'), 'port' => env('RABBITMQ_PORT', 5672), 'username' => env('RABBITMQ_USERNAME'), 'password' => env('RABBITMQ_PASSWORD'), 'vhost' => env('RABBITMQ_VHOST'), 'exchanges' => [ [ 'exchange' => 'exchange_name', 'exchange_type' => 'topic', 'exchange_passive' => false, 'exchange_durable' => true, 'exchange_auto_delete' => false, 'exchange_internal' => false, 'exchange_nowait' => false, 'exchange_properties' => [], 'routing' => [ 'routing.key.one', 'routing.key.two', ] ], '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']], 'timeout' => 0 ], ], ];

Register the Lumen Service Provider in bootstrap/app.php:, (*5)

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

//...

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

//...

Enable Facade Support for Lumen 5.2+, (*6)

$app->withFacades();

Laravel

Open config/app.php and add the service provider and alias:, (*7)

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

Setting up SSL

Make sure you copy the CA cert chain to somewhere that you can reference. The storage/certs directory is suggested. Also, don't forget to set the SSL settings in the config:, (*8)

'connect_options'     => [
    'capath' => '/etc/ssl/certs',
    'cafile' => env('RABBITMQ_SSL_CERT', storage_path() . '/certs/cacert.pem'),
    'verify_peer' => true
],

Also, don't forget that the port likely changed to 5671, (*9)

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']);

Notice that if you attempt to set properties like: x-message-ttl you may get an error like the following:, (*10)

AMQP-rabbit doesn't define data of type [], (*11)

You must specify a type:, (*12)

    Amqp::publish('routing-key', 'message', [
        'queue_properties' => [
            "x-ha-policy" => ["S", "all"],
            'x-message-ttl' => ['I', 86400000],
            'x-dead-letter-exchange' => ['S', 'orders_dead_letter'],
        ],
        'queue' => 'orders'
    ]);

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

  • This project is a fork of https://github.com/bschmitt/laravel-amqp
  • Some concepts were used from https://github.com/mookofe/tail

License

This package is open-sourced software licensed under the MIT license, (*13)

The Versions

29/06 2018

dev-master

9999999-dev

AMQP wrapper for Laravel and Lumen to publish and consume messages

  Sources   Download

MIT

The Requires

 

The Development Requires

by Björn Schmitt
by Ryan Hoshor

laravel lumen rabbitmq queue laravel5 package message queue amqp filld

29/06 2018

1.3.3

1.3.3.0

AMQP wrapper for Laravel and Lumen to publish and consume messages

  Sources   Download

MIT

The Requires

 

The Development Requires

by Björn Schmitt
by Ryan Hoshor

laravel lumen rabbitmq queue laravel5 package message queue amqp filld

29/06 2018

dev-develop

dev-develop

AMQP wrapper for Laravel and Lumen to publish and consume messages

  Sources   Download

MIT

The Requires

 

The Development Requires

by Björn Schmitt
by Ryan Hoshor

laravel lumen rabbitmq queue laravel5 package message queue amqp filld

28/06 2018

1.3.2

1.3.2.0

AMQP wrapper for Laravel and Lumen to publish and consume messages

  Sources   Download

MIT

The Requires

 

The Development Requires

by Björn Schmitt
by Ryan Hoshor

laravel lumen rabbitmq queue laravel5 package message queue amqp filld

27/06 2018

1.3.1

1.3.1.0

AMQP wrapper for Laravel and Lumen to publish and consume messages

  Sources   Download

MIT

The Requires

 

The Development Requires

by Björn Schmitt
by Ryan Hoshor

laravel lumen rabbitmq queue laravel5 package message queue amqp filld

27/11 2017

1.3.0

1.3.0.0

AMQP wrapper for Laravel and Lumen to publish and consume messages

  Sources   Download

MIT

The Requires

 

The Development Requires

by Björn Schmitt
by Ryan Hoshor

laravel lumen rabbitmq queue laravel5 package message queue amqp filld

15/08 2017

1.2.5

1.2.5.0

AMQP wrapper for Laravel and Lumen to publish and consume messages

  Sources   Download

MIT

The Requires

 

The Development Requires

by Björn Schmitt
by Ryan Hoshor

laravel lumen rabbitmq queue laravel5 package message queue amqp filld

15/08 2017

1.2.4

1.2.4.0

AMQP wrapper for Laravel and Lumen to publish and consume messages

  Sources   Download

MIT

The Requires

 

The Development Requires

by Björn Schmitt
by Ryan Hoshor

laravel lumen rabbitmq queue laravel5 package message queue amqp filld

08/08 2017

1.2.3

1.2.3.0

AMQP wrapper for Laravel and Lumen to publish and consume messages

  Sources   Download

MIT

The Requires

 

The Development Requires

by Björn Schmitt
by Ryan Hoshor

laravel lumen rabbitmq queue laravel5 package message queue amqp filld

22/09 2016

1.2.2

1.2.2.0

AMQP wrapper for Laravel and Lumen to publish and consume messages

  Sources   Download

MIT

The Requires

 

The Development Requires

by Björn Schmitt

laravel lumen rabbitmq queue laravel5 package message queue amqp björn schmitt bschmitt

29/06 2016

1.2.1

1.2.1.0

AMQP wrapper for Laravel and Lumen to publish and consume messages

  Sources   Download

MIT

The Requires

 

The Development Requires

by Björn Schmitt

laravel lumen rabbitmq queue laravel5 package message queue amqp björn schmitt bschmitt