PHPackages                             opsource/rabbitmq - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. opsource/rabbitmq

ActiveLibrary[Queues &amp; Workers](/categories/queues)

opsource/rabbitmq
=================

RabbitMQ client laravel

v1.0.3(1y ago)01.2k1MITPHPPHP &gt;=8.2.0

Since Dec 1Pushed 1y ago1 watchersCompare

[ Source](https://github.com/vahidaghazadeh/opsource-rabbitmq)[ Packagist](https://packagist.org/packages/opsource/rabbitmq)[ Docs](https://github.com/vahidaghazadeh/queryadapter)[ RSS](/packages/opsource-rabbitmq/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (6)Versions (4)Used By (1)

RabbitMQ Queue driver Laravel
=============================

[](#rabbitmq-queue-driver-laravel)

VersionLaravel supportLast update1.0.39-10-1126 June 2024Installation
============

[](#installation)

You can install this package using this composer:

```
composer require opsource/rabbitmq

```

Configuration
=============

[](#configuration)

After installation, the package will be published automatically You can find the package configuration file in the path `config/rabbitmq.php`

To use the package, you need to include the following environment variables in your project's ``.env'' file.

```
RABBITMQ_HOST=127.0.0.1
RABBITMQ_PORT=5672
RABBITMQ_USERNAME=admin
RABBITMQ_PASSWORD=admin
RABBITMQ_VHOST='/'
```

RabbitMQ Environment Variables
==============================

[](#rabbitmq-environment-variables)

This section outlines the RabbitMQ environment variables used to configure the connection and behavior of the RabbitMQ client. Optional environment variables that you can use to develop your application

VariableDescription`RABBITMQ_HOST`The hostname or IP address of the RabbitMQ server. Default: `127.0.0.1`.`RABBITMQ_PORT`The port number on which RabbitMQ is running. Default: `5672`.`RABBITMQ_USERNAME`The username for authenticating with RabbitMQ. Default: `admin`.`RABBITMQ_PASSWORD`The password for authenticating with RabbitMQ. Default: `admin`.`RABBITMQ_VHOST`The virtual host to use when connecting to RabbitMQ. Default: `/`.`RABBITMQ_WORKER`The worker mode for message delivery. Default: `DELIVERY_MODE_NON_PERSISTENT`.`RABBITMQ_CONNECTION`The name of the RabbitMQ connection. Default: `rabbitmq`.`RABBITMQ_MESSAGE_DELIVERY_MODE`The delivery mode for messages. Default: `DELIVERY_MODE_PERSISTENT`.`RABBITMQ_EXCHANGE_NAME`The name of the exchange. Default: `/`.`RABBITMQ_EXCHANGE_DECLARE`Whether to declare the exchange. Default: `false`.`RABBITMQ_EXCHANGE_TYPE`The type of exchange (e.g., direct, fanout, topic, headers). Default: `[direct fanout topic headers direct]`.`RABBITMQ_EXCHANGE_PASSIVE`Whether the exchange is passive. Default: `false`.`RABBITMQ_EXCHANGE_DURABLE`Whether the exchange should be durable. Default: `true`.`RABBITMQ_EXCHANGE_AUTO_DEL`Whether the exchange should be auto-deleted. Default: `false`.`RABBITMQ_EXCHANGE_INTERNAL`Whether the exchange is internal. Default: `false`.`RABBITMQ_EXCHANGE_NOWAIT`Whether to wait for the exchange to be declared. Default: `false`.`RABBITMQ_QUEUE_DECLARE`Whether to declare the queue. Default: `false`.`RABBITMQ_QUEUE_PASSIVE`Whether the queue is passive. Default: `false`.`RABBITMQ_QUEUE_DURABLE`Whether the queue should be durable. Default: `true`.`RABBITMQ_QUEUE_EXCLUSIVE`Whether the queue is exclusive. Default: `false`.`RABBITMQ_QUEUE_AUTO_DEL`Whether the queue should be auto-deleted. Default: `false`.`RABBITMQ_QUEUE_NOWAIT`Whether to wait for the queue to be declared. Default: `false`.`RABBITMQ_CONSUMER_TAG`The consumer tag. Default: `''` (empty string).`RABBITMQ_CONSUMER_NO_LOCAL`Whether to consume messages locally. Default: `false`.`RABBITMQ_CONSUMER_NO_ACK`Whether to acknowledge messages automatically. Default: `false`.`RABBITMQ_CONSUMER_EXCLUSIVE`Whether the consumer is exclusive. Default: `false`.`RABBITMQ_CONSUMER_NOWAIT`Whether to wait for the consumer. Default: `false`.`RABBITMQ_CONSUMER_SLEEP_MS`The sleep duration in milliseconds for the consumer. Default: `1000`.`RABBITMQ_QOS_ENABLED`Whether Quality of Service (QoS) is enabled. Default: `false`.`RABBITMQ_QOS_PREF_SIZE`The prefetch size for QoS. Default: `0`.`RABBITMQ_QOS_PREF_COUNT`The prefetch count for QoS. Default: `1`.`RABBITMQ_QOS_GLOBAL`Whether QoS is global. Default: `false`.These environment variables help configure the RabbitMQ connection and control various behaviors of the RabbitMQ client.

RabbitMQ Integration Documentation
==================================

[](#rabbitmq-integration-documentation)

This document outlines the usage of the RabbitMQManager in your application.

Initialization
--------------

[](#initialization)

First, create an instance of the `RabbitMQManager` class using the application container:

```
$rabbitMQ = new RabbitMQManager(app());
```

Alternatively, you can retrieve the RabbitMQ instance directly from the application container using the alias rabbitmq:

```
$rabbitMQ = app('rabbitmq');
```

Constructor Dependency Injection
================================

[](#constructor-dependency-injection)

To inject the RabbitMQManager into a class using constructor dependency injection, define your class constructor to accept a RabbitMQManager instance:

```
class YourClass {
    protected $rabbitMQ;

    // Constructor method
    public function __construct(RabbitMQManager $rabbitMQ) {
        $this->rabbitMQ = $rabbitMQ;
    }

    // Other methods...
}
```

Getting RabbitMQ Connections
============================

[](#getting-rabbitmq-connections)

You can retrieve the list of all RabbitMQ connections using the getConnections method provided by the RabbitMQ facade:

```
$connections = RabbitMQ::getConnections();
```

Complete Example
================

[](#complete-example)

Here’s a complete example of how to use RabbitMQManager in a class:

```
use App\Services\RabbitMQManager;
use Illuminate\Support\Facades\RabbitMQ;

class YourService {
    protected $rabbitMQ;

    public function __construct(RabbitMQManager $rabbitMQ) {
        $this->rabbitMQ = $rabbitMQ;
    }

    public function handle() {
        // Retrieve all RabbitMQ connections
        $connections = RabbitMQ::getConnections();

        // Perform operations using the $rabbitMQ instance
        // Example: $this->rabbitMQ->publishMessage($message);
    }
}

// Usage in a controller or another service
$service = new YourService(app('rabbitmq'));
$service->handle();
```

Key Points
==========

[](#key-points)

- Initialization: You can initialize the RabbitMQManager either directly or through the application container.
- Dependency Injection: Use constructor dependency injection to inject the RabbitMQManager into your classes.
- Connections: Retrieve all RabbitMQ connections using RabbitMQ::getConnections().

This documentation provides a clear and concise explanation of how to use the `RabbitMQManager` and related functionalities in your application. It covers initialization, dependency injection, and retrieving connections, along with a complete example for better understanding.

RabbitMQ Integration Documentation
==================================

[](#rabbitmq-integration-documentation-1)

This document provides an overview and examples of how to use RabbitMQ for publishing and consuming messages, resolving connections and channels, and configuring the RabbitMQ setup in your application.

Publishing Messages
-------------------

[](#publishing-messages)

### `Single Message Publishing`

[](#single-message-publishing)

To publish a single message to the default exchange/topic/queue:

```
$message = new RabbitMQMessage('message body');
```

Publish to the default exchange/topic/queue $rabbitMQ-&gt;publisher()-&gt;publish($message);

Bulk Messages
=============

[](#bulk-messages)

Publish multiple messages at once:

```
$messages = [
    new RabbitMQMessage('message 1'),
    new RabbitMQMessage('message 2')
];

$rabbitMQ->publisher()->publish($messages);
```

Consume Messages
================

[](#consume-messages)

### `Consume through a Closure`

[](#consume-through-a-closure)

Define a handler using a closure to consume messages:

```
$handler = new RabbitMQGenericMessageConsume(function (RabbitMQIncomingMessage $message) {
    $content = $message->getStream();
});
```

Consume through a Class
=======================

[](#consume-through-a-class)

Define a handler using a class to consume messages:

```
class MyMessageConsumer extends RabbitMQMessageConsumer {
    public function handle(RabbitMQIncomingMessage $message) {
        $content = $message->getStream();
    }
}

$handler = new MyMessageConsumer();

// Starts a blocking loop `while (true)`
$rabbitMQ->consumer()->consume($handler);
```

Interact with RabbitMQ
======================

[](#interact-with-rabbitmq)

### `Resolve the Default Connection`

[](#resolve-the-default-connection)

Get the default connection:

```
$amqpConnection = $rabbitMQ->resolveConnection();
```

Resolve the Default Channel
===========================

[](#resolve-the-default-channel)

Get the default channel:

```
$amqpChannel = $rabbitMQ->resolveChannel();
```

Configuration
=============

[](#configuration-1)

### `Connection Configuration`

[](#connection-configuration)

Override the default connection configuration:

```
$connectionName = 'custom_connection'; // Set to `null` for default connection
$connectionConfig = new ConnectionConfig(['username' => 'quest', 'password' => 'quest']);
$connectionConfig->setHost('localhost');
$customConnection = $rabbitMQ->resolveConnection($connectionName, $connectionConfig);
```

Message Configuration
=====================

[](#message-configuration)

Configure the message properties:

```
$config = [
    'content_encoding' => 'UTF-8',
    'content_type'     => 'text/plain',
    'delivery_mode'    => AMQPMessage::DELIVERY_MODE_PERSISTENT,
];
$message = new RabbitMQMessage('message body', $config);

// Set message exchange
$exchangeConfig = ['type' => AMQPExchangeType::DIRECT];
$exchange = new RabbitMQExchange('my_exchange', $exchangeConfig);
$message->setExchange($exchange);
```

Publish Configuration
=====================

[](#publish-configuration)

Configure the publisher and publish a message with specific settings:

```
$publisher = $rabbitMQ->publisher();
$message = new RabbitMQMessage('message body');

$exchangeConfig = ['type' => AMQPExchangeType::TOPIC];
$exchange = new RabbitMQExchange('my_exchange', $exchangeConfig);
$message->setExchange($exchange);

$routingKey = 'key'; // Can be an empty string, but not null
$connectionName = 'custom_connection'; // Set to null for default connection

$publishConfig = new PublishConfig(['exchange' => ['type' => AMQPExchangeType::FANOUT]]);
$publisher->publish($message, $routingKey, $connectionName, $publishConfig);
```

Consumer Configuration
======================

[](#consumer-configuration)

Configure the consumer settings:

```
$consumer = $rabbitMQ->consumer();
$routingKey = 'key';

$exchange = new RabbitMQExchange('test_exchange', ['declare' => true, 'durable' => true]);
$queue = new RabbitMQQueue('my_queue', ['declare' => true, 'durable' => true]);

$messageConsumer = new RabbitMQGenericMessageConsumer(
    function (RabbitMQIncomingMessage $message) {
        // Acknowledge a message
        $message->getDelivery()->acknowledge();
        // Reject a message
        $requeue = true; // Reject and Requeue
        $message->getDelivery()->reject($requeue);
    },
    $this,
);

// A1. Set the exchange and the queue directly
$messageConsumer
    ->setExchange($exchange)
    ->setQueue($queue);

// OR

// A2. Set the exchange and the queue through config
$consumeConfig = new ConsumeConfig(
  [
    'queue' => [
        'name' => 'my_queue',
        'declare' => true,
        'durable' => true,
    ],
    'exchange' => [
        'name' => 'test_exchange',
        'declare' => true,
    ],
  ],
);

$consumer->consume($messageConsumer, $routingKey, null, $consumeConfig);
```

Example
=======

[](#example)

### `Running a Consumer`

[](#running-a-consumer)

1. Create a Custom Command:

```
php artisan make:command MyRabbitConsumer --command "rabbitmq:my-consumer {--queue=} {--exchange=} {--routingKey=}"
```

2. Register the Command in `app/Console/Kernel.php`:

```
protected $commands = [
    MyRabbitConsumer::class,
];
```

3. Consume through the Handler:

```
