softonic / laravel-amqp
AMQP wrapper for Laravel and Lumen to publish and consume messages
Installs: 11 700
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 86
Open Issues: 0
Requires
- php: >=7.0
- illuminate/support: ^v9.0|^v10.0
- php-amqplib/php-amqplib: ^3.4.0
- vlucas/phpdotenv: ^5.5
Requires (Dev)
- mockery/mockery: ^1.5
- phpoption/phpoption: 1.9.x-dev
- phpunit/phpunit: ^8.0
- squizlabs/php_codesniffer: ^3.0@dev
- dev-master
- 2.4.0
- 2.3.0
- 2.2.0
- 2.1.0
- 2.1.0-alpha2
- 2.1.0-alpha
- 2.0.9
- 2.0.8
- 2.0.7.1
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- dev-dont-add-breaking-changes
- dev-feature/add-support-laravel-10
- dev-feature/remove_clear_conection_after_publish
- dev-feature/dont_close_connection_on_publish
- dev-feature/Allow-multiple-queue-bindings
- dev-bugfix/issue-56-ssl
- dev-feature/26-write-integration-tests
This package is auto-updated.
Last update: 2024-11-24 12:53:56 UTC
README
AMQP wrapper for Laravel and Lumen to publish and consume messages especially from RabbitMQ
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:
"softonic/laravel-amqp": "2.*" (Laravel >= 5.5) "softonic/laravel-amqp": "1.*" (Laravel < 5.5)
$ php composer update
or
$ php composer require softonic/laravel-amqp
Integration
Lumen
Create a config folder in the root directory of your Lumen application and copy the content from vendor/softonic/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' => 'username', 'password' => 'password', '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(Softonic\Amqp\LumenServiceProvider::class); //...
Add Facade Support for Lumen 5.2+
//... $app->withFacades(true, [ 'Softonic\Amqp\Facades\Amqp' => 'Amqp', ]); //...
Laravel
Open config/app.php and add the service provider and alias:
'Softonic\Amqp\AmqpServiceProvider',
'Amqp' => 'Softonic\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']);
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
- Package based on https://github.com/bschmitt/laravel-amqp
License
This package is open-sourced software licensed under the MIT license