usmonaliyev99/
laravel-simple-rabbitmq
g4t.io

Laravel Simple RabbitMQ Package

by Paul Redmond

The Simple RabbitMQ package for Laravel makes it simple to use RabbitMQ with support for multiple connections, easy publishing, and consumer mode. Learn how to…

README from usmonaliyev99/laravel-simple-rabbitmq Open on GitHub →

The package for simplified RabbitMQ usage, supporting multiple connections, easy publishing, and consumer mode.

Documentation

Key Features

  • Multiple Connections: Effortlessly manage multiple RabbitMQ connections within the same application.

  • Exchange supporting: You can push messages to exchanges

  • Message Publishing: Easily publish messages to queues and exchange with a fluent, chainable syntax.

  • Consumer Mode: Enable consumers to receive and process messages from queues in real time.

  • Manage queues and exchanges in config file: You can register queues and exchanges in config/simple-mq.php and define them in easy way which is amqp:define-queues command.

You can install the package via composer:

composer require usmonaliyev/laravel-simple-rabbitmq

Next step you must publish config and action register files:

php artisan vendor:publish --provider="Usmonaliyev\SimpleRabbit\SimpleRabbitMQServiceProvider"

As a result of this command, you will have a configuration file config/simple-mq.php and a registry file routes/amqp-handlers.php.

The config/simple-mq.php config file contains RabbitMQ connections with credentials, queues, default connection and default queue.

The next stage is configure .env file.

SIMPLE_MQ_CONNECTION=
SIMPLE_MQ_QUEUE=

SIMPLE_MQ_HOST=
SIMPLE_MQ_PORT=
SIMPLE_MQ_USERNAME=
SIMPLE_MQ_PASSWORD=

The package can publish and consume messages

Publishing

You can publish a message with default connection and default queue:

<?php

use Illuminate\Http\Request;
use Usmonaliyev\SimpleRabbit\Facades\SimpleMQ;

class FooController
{
    public function createFoo(Request $request)
    {
        // Something..
        
        SimpleMQ::queue('foo-queue')
          ->setBody(['name' => 'First Foo'])
          ->handler('create-foo')
          ->publish();
          
        return response()->json(['message' => 'OK']);
    }
}

Also, exchange function publish message to RabbitMq exchange:

<?php

namespace App\Https\Controllers;

use Illuminate\Http\Request;
use Usmonaliyev\SimpleRabbit\Facades\SimpleMQ;

class FooController
{
    public function createFoo(Request $request)
    {
        // Something..
        
        SimpleMQ::exchange('foo-exchange')
          ->setBody(['name' => 'First Foo'])
          ->setRoutingKey('foo.bar')
          ->handler('create-foo')
          ->publish();
          
        return response()->json(['message' => 'OK']);
    }
}

If you have multiply connection to RabbitMq, you can publish a message with connection method.

<?php

namespace App\Https\Controllers;

use Illuminate\Http\Request;
use Usmonaliyev\SimpleRabbit\Facades\SimpleMQ;

class FooController
{
    public function createFoo(Request $request)
    {
        // Something..
        
        SimpleMQ::connection('foo-connection')
          ->queue('foo-queue')
          ->setBody(['name' => 'First Foo'])
          ->handler('create-foo')
          ->publish();
          
        return response()->json(['message' => 'OK']);
    }
}

Consuming

Create app/AMQP/Handlers folder and create your handler classes.

For example:

<?php

namespace App\AMQP\Handlers;

use Usmonaliyev\SimpleRabbit\MQ\Message;

class FooHandler
{
    public function handle(Message $message)
    {
        // do something...
        
        $message->ack();
        
        return ['ok' => true];
    }
}

Don't forget acknowledge message end of process, else consumer does not accept next message.

Then register your handler in routes/amqp-handlers.php file.

<?php

use \App\AMQP\Handlers\FooHandler;
use \Usmonaliyev\SimpleRabbit\Facades\ActionMQ;

ActionMQ::register('create-foo', [FooHandler::class, 'handle']);

To consume messages use:

php artisan amqp:consume connection? queue?

The command requires two arguments which are connection and queue.
If you don't give them, command uses default connection and queue.

  • Exchange configuration in config/simple-mq.php
  • Setup testing.
composer test

The MIT License.

More from the ecosystem

marcreichel/
laya-php
g4t.io
3 53

LayaPHP: Self-Hosted Text Classification for PHP and Laravel

Classify text in PHP without an LLM bill: typed decisions in 100+ languages, self-hosted. Laravel-ready SDK for Laya, a Jev AI alternative.

ai classification decision-engine
marcreichel/laya-php via Laravel News
Josh-Dovey/
postcodes-laravel
g4t.io
5 9

Postcodes for Laravel: GB Postcode Lookup and Geography Data

Postcodes for Laravel adds typed GB postcode lookups, validation, geography data, distance searches, and test fakes through the GB Postcodes API.

Josh-Dovey/postcodes-laravel via Laravel News
RedberryProducts/
mailbox-for-laravel
g4t.io
4 115

Mailbox for Laravel: Preview and Test Rendered Email

Mailbox for Laravel captures outgoing mail in a local dashboard and lets you test rendered HTML, recipients, and attachments with fluent assertions.

RedberryProducts/mailbox-for-laravel via Laravel News