johndev / hermes
A simple message broker package
Requires
- php: >=7.4
- illuminate/config: ^8.50
- illuminate/console: ^8.50
- php-amqplib/php-amqplib: ^3.0
Requires (Dev)
- phpunit/phpunit: ~9.0
This package is auto-updated.
Last update: 2024-10-29 06:19:07 UTC
README
Hermes is a simple wrapper for popular message brokers
Installation
Require this package with composer.
composer require johndev/hermes
This package use the Laravel Package Auto-Discovery, so is not required you to manually add the ServiceProvider.
No auto-discovery is available
If you don't use Laravel auto-discovery, add the ServiceProvider to the providers array in config/app.php.
Hermes\Providers\HermesServiceProvider::class,
If you want to use the Hermes facade, add this to your facades array in config/app.php.
'Hermes' => Hermes\Facades\Hermes::class,
Configuration file
To publish the configuration file for hermes execute this command into your project.
php artisan vendor:publish --provider="JohnDev\Hermes\Providers\HermesServiceProvider"
Configuration
Default broker
To configure the default message broker used by the package, change this in your environment file.
HERMES_DEFAULT_BROKER="amqp"
AMQP Message broker configuration
Usage
Publish
Publish a message
<?php use Hermes\Facades\Hermes; $bindingKey = 'co.johndev.test'; $message = 'Test message'; Hermes::publish($bindingKey, $message);
Consume
Consume a message and finish
<?php use JohnDev\Hermes\Message; use JohnDev\Hermes\Contracts\CarrierContract; Hermes::consume(function(Message $message, CarrierContract $carrier) { dump($message->body()); $message->ack(); $carrier->finish(); });
Consume a message with different queue
<?php use JohnDev\Hermes\Facades\Hermes; use JohnDev\Hermes\Message; use JohnDev\Hermes\Contracts\CarrierContract; Hermes::queue('queue-name')->consume(function(Message $message, CarrierContract $carrier) { dump($message->body()); $message->ack(); $carrier->finish(); });
Customize configuration in execution time
If you want to customize the configuration in execution time, use the config() method available in Hermes facade
<?php use JohnDev\Hermes\Facades\Hermes; use JohnDev\Hermes\Message; use JohnDev\Hermes\Contracts\CarrierContract; $config = [ 'consume' => [ 'tag' => 'custom-tag', 'timeout' => 10, ] ]; Hermes::config($config)->consume(function(Message $message, CarrierContract $carrier) { dump($message->body()); $message->ack(); $carrier->finish(); });