PHPackages                             anik/laravel-amqp - 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. anik/laravel-amqp

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

anik/laravel-amqp
=================

anik/amqp wrapper for Laravel-ish frameworks

v2.1(1y ago)33192.8k↓16.3%4[1 issues](https://github.com/ssi-anik/laravel-amqp/issues)2MITPHPPHP ^8.1CI passing

Since Nov 16Pushed 1y ago2 watchersCompare

[ Source](https://github.com/ssi-anik/laravel-amqp)[ Packagist](https://packagist.org/packages/anik/laravel-amqp)[ RSS](/packages/anik-laravel-amqp/feed)WikiDiscussions master Synced 1mo ago

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

anik/laravel-amqp [![codecov](https://camo.githubusercontent.com/c066406538f30bff7f8fadf2fd9ab78d46291563c71fd1a54d4208759c4aafa9/68747470733a2f2f636f6465636f762e696f2f67682f7373692d616e696b2f6c61726176656c2d616d71702f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d49654a777871514f7544)](https://codecov.io/gh/ssi-anik/laravel-amqp)[![PHP Version Require](https://camo.githubusercontent.com/57e1547d4d50287da03e7353084ea7e2d385609481c55c0323bf11bab15fba0a/687474703a2f2f706f7365722e707567782e6f72672f616e696b2f6c61726176656c2d616d71702f726571756972652f706870)](//packagist.org/packages/anik/laravel-amqp)[![Latest Stable Version](https://camo.githubusercontent.com/87c5a40c5c272d549de3665cf17cddd9c41aa8c237f46731dcb45b27a7b414b6/68747470733a2f2f706f7365722e707567782e6f72672f616e696b2f6c61726176656c2d616d71702f76)](//packagist.org/packages/anik/laravel-amqp)[![Total Downloads](https://camo.githubusercontent.com/ee0828a7af094a950b310efcc2876b41b441d2fde60f04662d85fdff10270ad1/68747470733a2f2f706f7365722e707567782e6f72672f616e696b2f6c61726176656c2d616d71702f646f776e6c6f616473)](//packagist.org/packages/anik/laravel-amqp)[![Jetbrains](https://camo.githubusercontent.com/7270fc509043f1117172e944c5f3d92142175d5c468a9fc7f2738b2b27fd31b0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f2d4a6574427261696e732d3030303f7374796c653d666c6174266c6f676f3d6a6574627261696e73)](https://jb.gg/OpenSourceSupport)
==============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#aniklaravel-amqp)

[anik/amqp](https://packagist.org/packages/anik/amqp) wrapper for Laravel-ish frameworks.

- [Laravel](https://github.com/laravel/laravel)
- [Lumen](https://github.com/laravel/lumen)
- [Laravel Zero](https://github.com/laravel-zero/laravel-zero)

Examples
========

[](#examples)

Checkout the [repository](https://github.com/ssi-anik/laravel-rabbitmq-producer-consumer-example) for example.

Documentation
=============

[](#documentation)

Installation
------------

[](#installation)

To install the package, run

> composer require anik/laravel-amqp

### Laravel

[](#laravel)

The `Anik\Laravel\Amqp\Providers\AmqpServiceProvider::class` service provider should automatically get registered. If not, then you can manually add the service provider in your `config/app.php` providers array:

```
'providers' => [
    // ...
    Anik\Laravel\Amqp\Providers\AmqpServiceProvider::class,
]
```

- Publish configuration with `php artisan vendor:publish --provider "Anik\Laravel\Amqp\Providers\AmqpServiceProvider"`command.

### Lumen

[](#lumen)

- Register `Anik\Laravel\Amqp\Providers\AmqpServiceProvider::class` service provider in your `bootstrap/app.php` file.

```
$app->register(Anik\Laravel\Amqp\Providers\AmqpServiceProvider::class);
```

- Copy configuration `amqp.php` in your config directory from `vendor/anik/laravel-amqp/src/config/amqp.php`.
- Import your configuration using `$app->configure('amqp');` in your `bootstrap/app.php`.

### Laravel Zero

[](#laravel-zero)

- Register `Anik\Laravel\Amqp\Providers\AmqpServiceProvider::class` service provider in your `config/app.php` providers array:

```
'providers' => [
    /// ...
    Anik\Laravel\Amqp\Providers\AmqpServiceProvider::class,
]
```

- Copy configuration `amqp.php` in your config directory from `vendor/anik/laravel-amqp/src/config/amqp.php`.

Configuration
-------------

[](#configuration)

In your `config/amqp.php`, you can define multiple connections and use them from your code by pointing the connection name.

- `amqp.default` denoting the default connection. Will be used if no connection is specified when producing or consuming messages.
- `amqp.connections.*.connection.class` denoting the underlying Amqp connection to be used. By default, it uses lazy connection. You can change it to any implementation of `PhpAmqpLib\Connection\AbstractConnection`.
- `amqp.connections.*.connection.hosts` can have multiple host configuration. Each host config must contain `host`, `port`, `user`, `password` keys. It can also contain `vhost` which is optional. Lazy connections cannot have **more than one host** configuration otherwise it'll throw error.
- You can also pass optional array of parameters through `amqp.connections.*.connection.options` when creating an instance of `amqp.connections.*.connection.class` internally.
- `amqp.connections.*.message` holds the default properties of a message when publishing.
- `amqp.connections.*.exchange` holds the default properties of your exchange when publishing &amp; consuming.
- `amqp.connections.*.queue` holds the default properties of your queue when consuming.
- `amqp.connections.*.consumer` holds the default properties of consumer when consuming.
- `amqp.connections.*.qos` holds the default properties of QoS when consuming.

### Octane support

[](#octane-support)

This package supports laravel octane by default. [To keep the AMQP connection alive](https://www.cloudamqp.com/blog/part4-rabbitmq-13-common-errors.html), you have to configure octane to `warm` the connection, by adding **'amqp'** to the warm array in octane configurations.

```
// config/octane.php
// ...
'warm' => [
    // ...
    'amqp', // publish($messages); // publishes to rabbitmq connection

app('amqp')->publish($messages); // publishes to default connection
app('amqp')->connection('rabbitmq')->publish($messages); // publishes to rabbitmq connection

app()->make('amqp')->publish($messages); // publishes to default connection
app()->make('amqp')->connection('rabbitmq')->publish($messages); // publishes to rabbitmq connection

/** @var \Anik\Laravel\Amqp\AmqpManager $amqpManager */
$amqpManager->publish($messages); // publishes to default connection
$amqpManager->connection('rabbitmq')->publish($messages); // publishes to rabbitmq connection

Amqp::consume(function(ConsumableMessage $message) {
    var_dump($message->getMessageBody());
    $message->ack();
}); // consumes from default connection
Amqp::connection('rabbitmq')->consume(function(ConsumableMessage $message) {
    var_dump($message->getMessageBody());
    $message->ack();
}); // consumes from rabbitmq connection

app('amqp')->consume(function(ConsumableMessage $message) {
    var_dump($message->getMessageBody());
    $message->ack();
}); // consumes from default connection
app('amqp')->connection('rabbitmq')->consume(function(ConsumableMessage $message) {
    var_dump($message->getMessageBody());
    $message->ack();
}); // consumes from rabbitmq connection

app()->make('amqp')->consume(function(ConsumableMessage $message) {
    var_dump($message->getMessageBody());
    $message->ack();
}); // consumes from default connection
app()->make('amqp')->connection('rabbitmq')->consume(function(ConsumableMessage $message) {
    var_dump($message->getMessageBody());
    $message->ack();
}); // consumes from rabbitmq connection

/** @var \Anik\Laravel\Amqp\AmqpManager $amqpManager */
$amqpManager->consume(function(ConsumableMessage $message) {
    var_dump($message->getMessageBody());
    $message->ack();
}); // consumes from default connection
$amqpManager->connection('rabbitmq')->consume(function(ConsumableMessage $message) {
    var_dump($message->getMessageBody());
    $message->ack();
}); // consumes from rabbitmq connection
```

### Note

[](#note)

In this documentation, it'll use **FACADE** afterwards. If you're using **Lumen**, then you can use other approaches. The package **doesn't require enabling Facade**.

Publishing messages
-------------------

[](#publishing-messages)

To publish messages,

```
use Anik\Laravel\Amqp\Facades\Amqp;

Amqp::publish($messages, $routingKey, $exchange, $options);
Amqp::connection('rabbitmq')->publish($messages, $routingKey, $exchange, $options);
```

- `$messages` Type: `mixed`. **Required**. It can be a single message, or an array of messages of any scalar type or implementation of `Anik\Amqp\Producible`.
- `$routingKey` Type: `string`. **Optional**. Default: `''` (empty string).
- `$exchange` Type: `null | Anik\Amqp\Exchanges\Exchange`. **Optional**. Default: `null`.
- `$options` Type: `array`. **Optional**. Default: `[]`.
    - Key `message` - Accepts: `array`. Valid properties for `PhpAmqpLib\Message\AMQPMessage`.
    - Key `exchange` - Accepts: `array`. Refer to `amqp.connections.*.exchange`.
    - Key `publish` - Accepts: `array`. Refer to [`Anik\Amqp\Producer::publishBatch`](https://github.com/ssi-anik/amqp#documentation)

### Note

[](#note-1)

- If any of the `$messages` is not an implementation of `Anik\Amqp\Producible`, then that message will be converted to `Anik\Amqp\Producible` using `Anik\Amqp\ProducibleMessage`.
- When converting to `Anik\Amqp\Producible`, it'll try to use `$options['message']` as the message property. If not set, it'll then try to use `amqp.connections.*.message` properties if available.
- If `$exchange` is set to `null`, it'll check if `$options['exchange']` is set or not. If not set, it'll then use `amqp.connections.*.exchange` properties if available.
- If `$options['publish']` is not set, it'll try to use `amqp.connections.*.publish` properties if available.

Consuming messages
------------------

[](#consuming-messages)

To consume messages,

```
use Anik\Laravel\Amqp\Facades\Amqp;

Amqp::consume($handler, $bindingKey, $exchange, $queue, $qos , $options);
Amqp::connection('rabbitmq')->consume($handler, $bindingKey, $exchange, $queue, $qos , $options);
```

- `$handler` Type: `callable | Anik\Amqp\Consumable`. **Required**.
- `$bindingKey` Type: `string`. **Optional**. Default: `''` (empty string).
- `$exchange` Type: `null | Anik\Amqp\Exchanges\Exchange`. **Optional**. Default: `null`.
- `$queue` Type: `null | Anik\Amqp\Queues\Queue`. **Optional**. Default: `null`.
- `$qos` Type: `null | Anik\Amqp\Qos\Qos`. **Optional**. Default: `null`.
- `$options` Type: `array`. **Optional**. Default: `[]`.
    - Key `exchange` - Accepts: `array`. Refer to `amqp.connections.*.exchange`.
    - Key `queue` - Accepts: `array`. Refer to `amqp.connections.*.queue`.
    - Key `qos` - Accepts: `array`. Refer to `amqp.connections.*.qos`.
    - Key `consumer` - Accepts: `array`. Refer to `amqp.connections.*.consumer`.
    - Key `bind` - Accepts: `array`. Refer to [`Anik\Amqp\Consumer::consume`](https://github.com/ssi-anik/amqp#documentation)

### Note

[](#note-2)

- If `$handler` is not an implementation of `Anik\Amqp\Consumable`, then the handler will be converted to `Anik\Amqp\Consumable` using `Anik\Amqp\ConsumableMessage`.
- If `$exchange` is set to `null`, it'll check if `$options['exchange']` is set or not. If not set, it'll then use `amqp.connections.*.exchange` properties if available.
- If `$queue` is set to `null`, it'll check if `$options['queue']` is set or not. If not set, it'll then use `amqp.connections.*.queue` properties if available.
- If `$qos` is set to `null`, it'll check if `$options['qos']` is set or not. If not set, it'll then use `amqp.connections.*.qos` properties if `amqp.connections.*.qos.enabled` is set to a **truthy** value.
- If `$options['bind']` is not set, it'll use `amqp.connections.*.bind` properties if available.
- If `$options['consumer']` is not set, it'll use `amqp.connections.*.consumer` properties if available.

Testing
-------

[](#testing)

The package allows asserting a few scenarios. Before you can run those assertions, you'll need use `Amqp::fake()`.

```
