PHP code example of batatahub-tech / laravel-rabbitmq

1. Go to this page and download the library: Download batatahub-tech/laravel-rabbitmq library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

batatahub-tech / laravel-rabbitmq example snippets


return [
    'connections' => [
        'default' => [
            'host' => env('RABBITMQ_HOST', '127.0.0.1'),
            'port' => env('RABBITMQ_PORT', 5672),
            'username' => env('RABBITMQ_USER', 'guest'),
            'password' => env('RABBITMQ_PASS', 'guest'),
            'vhost' => env('RABBITMQ_VHOST', '/'),
        ],
        'another' => [
            'host' => env('RABBITMQ_ANOTHER_HOST', '127.0.0.1'),
            'port' => env('RABBITMQ_ANOTHER_PORT', 5672),
            'username' => env('RABBITMQ_ANOTHER_USER', 'guest'),
            'password' => env('RABBITMQ_ANOTHER_PASS', 'guest'),
            'vhost' => env('RABBITMQ_ANOTHER_VHOST', '/'),
        ],
    ],

    'consumers' => [
        [
            'connection' => 'default',
            'queue' => 'UserCreatedQueue',
            'handler' => \App\RabbitMQ\Consumers\UserCreatedConsumer::class,
            // 'noAck' => false,   // default: broker waits for explicit ack
            // 'exclusive' => false,
            // 'nowait' => false,
            // 'consumerTag' => 'my_consumer_tag',
            // 'arguments' => [ 'x-retries' => 3 ],
            // 'ticket' => 42,
        ],

        // Example mapped to another connection
        [
            'connection' => 'another',
            'queue' => 'EmailQueue',
            'handler' => \App\RabbitMQ\Consumers\EmailConsumer::class,
            'noAck' => true,     // auto-ack at broker (handler won't need to ack)
        ],
    ],
];



namespace App\RabbitMQ\Consumers;

use PhpAmqpLib\Message\AMQPMessage;

class UserCreatedConsumer
{
    public function handle(AMQPMessage $message): void
    {
        try {
            $payload = json_decode($message->getBody(), true);

            // Process your payload...

            // Confirm to the broker that processing succeeded
            $message->ack();
        } catch (\Throwable $e) {
            // Reject; requeue=true. Adjust to your retry policy.
            $message->nack(false, true);
            throw $e;
        }
    }
}



use BatataHub\RabbitMQ\Services\RabbitMQService;

class UserController
{
    public function store(RabbitMQService $rabbit)
    {
        // Setup topology if needed
        $rabbit
            ->declareExchange('users', 'topic')
            ->declareQueue('UserCreatedQueue')
            ->bindQueue('UserCreatedQueue', 'users', 'user.created')

            // Publish payload to the exchange with routing key
            ->publish('users', 'user.created', [
                'id' => 123,
                'name' => 'Jane',
            ]);

        // ...
    }
}



use BatataHub\RabbitMQ\Services\RabbitMQService;
use PhpAmqpLib\Message\AMQPMessage;

class Worker
{
    public function __invoke(RabbitMQService $rabbit): void
    {
        $rabbit->consume('UserCreatedQueue', function (AMQPMessage $message) {
            $data = json_decode($message->getBody(), true);

            // ... process ...

            $message->ack();
        });

        $rabbit->startConsuming();
    }
}
bash
# Publish config/rabbitmq.php
php artisan vendor:publish --tag=config --provider="BatataHub\\RabbitMQ\\Providers\\RabbitMQServiceProvider"

# Publish the consumer stub to stubs/rabbitmq-consumer.plain.stub
php artisan vendor:publish --tag=stubs --provider="BatataHub\\RabbitMQ\\Providers\\RabbitMQServiceProvider"
bash
php artisan rabbitmq:make-consumer UserCreatedConsumer
# or
php artisan rabbitmq:make-consumer Sub/Path/EmailConsumer
bash
php artisan rabbitmq:consume default
bash
php artisan rabbitmq:consume default UserCreatedQueue