johndev/hermes

A simple message broker package

0.1.6-BETA 2021-07-29 05:05 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();
});