PHPackages                             kunalvarma05/laravel-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. kunalvarma05/laravel-rabbitmq

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

kunalvarma05/laravel-rabbitmq
=============================

Work with RabbitMQ in Laravel.

1.5.0(2y ago)1856.4k↓71.6%6[4 issues](https://github.com/kunalvarma05/laravel-rabbitmq/issues)[1 PRs](https://github.com/kunalvarma05/laravel-rabbitmq/pulls)MITPHPPHP ^8.1

Since Feb 24Pushed 2y ago2 watchersCompare

[ Source](https://github.com/kunalvarma05/laravel-rabbitmq)[ Packagist](https://packagist.org/packages/kunalvarma05/laravel-rabbitmq)[ Docs](https://github.com/kunalvarma05/laravel-rabbitmq)[ RSS](/packages/kunalvarma05-laravel-rabbitmq/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (5)Versions (20)Used By (0)

Laravel RabbitMQ
================

[](#laravel-rabbitmq)

[![Latest Version on Packagist](https://camo.githubusercontent.com/e0d3d4f638c3b648fd8345bd7085fdb89167c1008226308e7650d4f0040723eb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b756e616c7661726d6130352f6c61726176656c2d7261626269746d712e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/kunalvarma05/laravel-rabbitmq)[![Travis (.com)](https://camo.githubusercontent.com/ffaeda1f109f9ab9ce78915b8d6ae7288222d9bc5b1baceace8e8b2b1f3af67d/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636f6d2f6b756e616c7661726d6130352f6c61726176656c2d7261626269746d713f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/ffaeda1f109f9ab9ce78915b8d6ae7288222d9bc5b1baceace8e8b2b1f3af67d/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636f6d2f6b756e616c7661726d6130352f6c61726176656c2d7261626269746d713f7374796c653d666c61742d737175617265)[![Coveralls github](https://camo.githubusercontent.com/e2fb0eb1d291912c6fdb276567eb0955ac17e6ffa41b2e225d7fe88585ed0fd1/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6769746875622f6b756e616c7661726d6130352f6c61726176656c2d7261626269746d713f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/e2fb0eb1d291912c6fdb276567eb0955ac17e6ffa41b2e225d7fe88585ed0fd1/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6769746875622f6b756e616c7661726d6130352f6c61726176656c2d7261626269746d713f7374796c653d666c61742d737175617265)[![StyleCI](https://camo.githubusercontent.com/9a3adfad3a747801c66669993f1ea15fbc55994aadc9940f35ff7003d6e02b2e/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3234323334383132372f736869656c64)](https://github.styleci.io/repos/242348127)[![Quality Score](https://camo.githubusercontent.com/49316b75f93a42dada1c9370271eff07f00a068669294f6a97c6f42b73300491/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6b756e616c7661726d6130352f6c61726176656c2d7261626269746d712e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/kunalvarma05/laravel-rabbitmq)[![Total Downloads](https://camo.githubusercontent.com/dde19fee187dfec513073b2a7a7cee28715bb7a9a98e4e87cb3927e14da148c0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6b756e616c7661726d6130352f6c61726176656c2d7261626269746d712e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/kunalvarma05/laravel-rabbitmq)

An easy-to-use Laravel package for working with RabbitMQ.

Features
--------

[](#features)

- Producers
- Consumers
- Publish / Subscribe
- Exchanges
    - Default
    - Direct
    - Topic
    - Fanout

Requirements
------------

[](#requirements)

- PHP 7.4+
- Laravel 6.0+

Setup
-----

[](#setup)

### 1. Installation

[](#1-installation)

```
composer require kunalvarma05/laravel-rabbitmq
```

### 2. Default Configuration

[](#2-default-configuration)

```
php artisan vendor:publish --tag=config
```

Quick Start
-----------

[](#quick-start)

### **Initialize**

[](#initialize)

There are multiple ways of initializing the library:

```
// A. Direct instantiation
$rabbitMQ = new RabbitMQManager(app());

// B. Binding
$rabbitMQ = app('rabbitmq');

// C. Dependency injection (Controller, Command, Job, etc.)
public function __consturct(RabbitMQManager $rabbitMQ) { ... }

// D. Facade
// All the public methods of the `RabbitMQManager` class
// are available through the `RabbitMQ` facade.
RabbitMQ::getConnections();
```

### **Publish**

[](#publish)

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

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

// Publish bulk messages
$messages = [new RabbitMQMessage('message 1'), new RabbitMQMessage('message 2')];
$rabbitMQ->publisher()->publish($messages);
```

### **Consume**

[](#consume)

```
// A. Consume through a closure
$handler = new RabbitMQGenericMessageConsume(function (RabbitMQIncomingMessage $message) {
  $content = $message->getStream();
});

// B. Consume through a class
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**

[](#interact)

```
// Resolve the default connection
// @see: AMQPSSLConnection https://github.com/php-amqplib/php-amqplib/blob/master/PhpAmqpLib/Connection/AMQPSSLConnection.php
$amqpConnection = $rabbitMQ->resolveConnection();

// Resolve the default channel
// @see: AMQPChannel https://github.com/php-amqplib/php-amqplib/blob/master/PhpAmqpLib/Channel/AMQPChannel.php
$amqpChannel = $rabbitMQ->resolveChannel();
```

### **Configuration**

[](#configuration)

#### Connection Configuration

[](#connection-configuration)

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

#### Message Configuration

[](#message-configuration)

```
$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)

```
$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

// The publish config allows you to any override default configuration
//
// The following precendence works for the configuration:
// Message exchange config > Publish config > Connection config > Default config
//
// In this case, the exchange type used would be AMQPExchangeType::TOPIC
$publishConfig = new PublishConfig(['exchange' => ['type' => AMQPExchangeType::FANOUT]]);

$publisher->publish($message, $routingKey, $connectionName, $publishConfig);
```

#### Consumer Configuration

[](#consumer-configuration)

```
$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)

- Create a custom command:

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

- Register the command in `app/Console/Kernel.php`

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

- Consume through the handler

```
