vladmeh/rabbitmq-client

Laravel RabbitMQ Client

2.0.3 2021-02-11 11:38 UTC

This package is auto-updated.

Last update: 2024-04-11 18:40:46 UTC


README

Build Status Codacy Badge StyleCI Latest Stable Version Total Downloads License

vladmeh/rabbitmq-client

Wrapper to php-amqplib library for publishing and consuming RabbitMQ messages using Laravel framework

Features

PHP8.0 support will be available after php-amqplib is updated to the next major version 3.0. (php-amqplib/php-amqplib#858)

Version Compatibility

Laravel Rabbit Client
6.x 1.x
7.x 2.x
8.x --

Installation

Composer

$ composer require vladmeh/rabbit-client 

or add the following to your requirement part within the composer.json:

{
    "require": {
        "vladmeh/rabbitmq-client": "^2.*"
    }
}

Laravel will automatically register service provider (Vladmeh\RabbitMQ\RabbitMQClientProvider) and facade when is installed

Configure

Add these properties to .env with proper values:

RABBITMQ_HOST=localhost
RABBITMQ_PORT=5672
RABBITMQ_USER=guest
RABBITMQ_PASSWORD=guest

If you need advanced configuration properties run:

$ php artisan vendor:publish --tag=rabbit

This command will create a config file \config\rabbit.php

Integration

Producer

Publish a message in the existing queue

Rabbit::publish('message', '', 'queue-name');

Publish a message in the existing exchange

Rabbit::publish('message', 'exchange-name', 'routing-key');

Publish a message, with custom settings

Rabbit::publish('message', 'amq.fanout', '', [
    'hosts' => [
        'vhosts' => 'vhost3'
    ],
    'message' => [
        'content_type' => 'application/json',
    ],
    'exchange_declare' => [
        'type' => 'fanout',
        'auto_delete' => true,
    ]
]);

All default settings are defined in \config\rabbit.php.

Consumer

Consume messages to an existing queue

Rabbit::consume('queue-name', function (AMQPMessage $msg) {
    $msg->ack();
    var_dump($msg->body);
    if ($msg->getMessageCount() === null) {
        $msg->getChannel()->basic_cancel($msg->getConsumerTag());
    }
});

RPC client

$response = Rabbit::rpc('message', 'queue-name', ['connection' => [
    'read_write_timeout' => 10.0,
    'channel_rpc_timeout' => 10.0
]]);

var_dump($response);