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

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

websolutionfalcon/laravel-queue-rabbitmq
========================================

RabbitMQ driver for Laravel Queue. Supports Laravel Horizon.

v14.0.0(4mo ago)01.4k↓40.7%MITPHPPHP ^8.0

Since Jan 14Pushed 4mo agoCompare

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

READMEChangelogDependencies (9)Versions (2)Used By (0)

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

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

[![Latest Stable Version](https://camo.githubusercontent.com/6a151a20f106a73c9dd13b9ac1570904c5b18bf2c11b6c40ec0bf8e91ae7aa6f/68747470733a2f2f706f7365722e707567782e6f72672f776562736f6c7574696f6e66616c636f6e2f6c61726176656c2d71756575652d7261626269746d712f762f737461626c653f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/websolutionfalcon/laravel-queue-rabbitmq)[![Build Status](https://github.com/vyuldashev/laravel-queue-rabbitmq/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/vyuldashev/laravel-queue-rabbitmq/actions/workflows/tests.yml)[![Total Downloads](https://camo.githubusercontent.com/69d94cc27ba6559c013598cfdafd51fbc3999199933c3f3757d0d29954b332d8/68747470733a2f2f706f7365722e707567782e6f72672f776562736f6c7574696f6e66616c636f6e2f6c61726176656c2d71756575652d7261626269746d712f646f776e6c6f6164733f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/websolutionfalcon/laravel-queue-rabbitmq)[![License](https://camo.githubusercontent.com/35d5d5528b7d24d510d24bd4e152d2fba1547639a2cbce0db4caf4230081ef04/68747470733a2f2f706f7365722e707567782e6f72672f776562736f6c7574696f6e66616c636f6e2f6c61726176656c2d71756575652d7261626269746d712f6c6963656e73653f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/websolutionfalcon/laravel-queue-rabbitmq)

Support Policy
--------------

[](#support-policy)

Only the latest version will get new features. Bug fixes will be provided using the following scheme:

Package VersionLaravel VersionBug Fixes Until139August 8th, 2023[Documentation](https://github.com/vyuldashev/laravel-queue-rabbitmq/blob/master/README.md)Installation
------------

[](#installation)

You can install this package via composer using this command:

```
composer require websolutionfalcon/laravel-queue-rabbitmq

```

The package will automatically register itself.

### Configuration

[](#configuration)

Add connection to `config/queue.php`:

> This is the minimal config for the rabbitMQ connection/driver to work.

```
'connections' => [
    // ...

    'rabbitmq' => [

       'driver' => 'rabbitmq',
       'hosts' => [
           [
               'host' => env('RABBITMQ_HOST', '127.0.0.1'),
               'port' => env('RABBITMQ_PORT', 5672),
               'user' => env('RABBITMQ_USER', 'guest'),
               'password' => env('RABBITMQ_PASSWORD', 'guest'),
               'vhost' => env('RABBITMQ_VHOST', '/'),
           ],
           // ...
       ],

       // ...
    ],

    // ...
],
```

### Optional Queue Config

[](#optional-queue-config)

Optionally add queue options to the config of a connection. Every queue created for this connection, gets the properties.

When you want to prioritize messages when they were delayed, then this is possible by adding extra options.

- When max-priority is omitted, the max priority is set with 2 when used.

```
'connections' => [
    // ...

    'rabbitmq' => [
        // ...

        'options' => [
            'queue' => [
                // ...

                'prioritize_delayed' =>  false,
                'queue_max_priority' => 10,
            ],
        ],
    ],

    // ...
],
```

When you want to publish messages against an exchange with routing-keys, then this is possible by adding extra options.

- When the exchange is omitted, RabbitMQ will use the `amq.direct` exchange for the routing-key
- When routing-key is omitted the routing-key by default is the `queue` name.
- When using `%s` in the routing-key the queue\_name will be substituted.

> Note: when using an exchange with routing-key, you probably create your queues with bindings yourself.

```
'connections' => [
    // ...

    'rabbitmq' => [
        // ...

        'options' => [
            'queue' => [
                // ...

                'exchange' => 'application-x',
                'exchange_type' => 'topic',
                'exchange_routing_key' => '',
            ],
        ],
    ],

    // ...
],
```

In Laravel failed jobs are stored into the database. But maybe you want to instruct some other process to also do something with the message. When you want to instruct RabbitMQ to reroute failed messages to a exchange or a specific queue, then this is possible by adding extra options.

- When the exchange is omitted, RabbitMQ will use the `amq.direct` exchange for the routing-key
- When routing-key is omitted, the routing-key by default the `queue` name is substituted with `'.failed'`.
- When using `%s` in the routing-key the queue\_name will be substituted.

> Note: When using failed\_job exchange with routing-key, you probably need to create your exchange/queue with bindings yourself.

```
'connections' => [
    // ...

    'rabbitmq' => [
        // ...

        'options' => [
            'queue' => [
                // ...

                'reroute_failed' => true,
                'failed_exchange' => 'failed-exchange',
                'failed_routing_key' => 'application-x.%s',
            ],
        ],
    ],

    // ...
],
```

### Delayed Messages

[](#delayed-messages)

This package supports two strategies for handling delayed messages (jobs that should be processed after a specific time):

#### 1. Dead Letter Exchange (DLX) Strategy (Default)

[](#1-dead-letter-exchange-dlx-strategy-default)

The DLX strategy uses RabbitMQ's native TTL (Time-To-Live) and Dead Letter Exchange features. This is the **default strategy** and works with all RabbitMQ versions without requiring any plugins.

**How it works:**

- Creates temporary queues with TTL for each unique delay time
- Messages expire in the delay queue and are routed to the main queue
- Queues auto-delete after messages are processed

**Configuration:**

```
'connections' => [
    'rabbitmq' => [
        // ...

        'options' => [
            'queue' => [
                'delay_strategy' => 'dlx', // This is the default, can be omitted
            ],
        ],
    ],
],
```

#### 2. Delayed Message Exchange Plugin Strategy

[](#2-delayed-message-exchange-plugin-strategy)

The plugin strategy uses the official [`rabbitmq_delayed_message_exchange`](https://github.com/rabbitmq/rabbitmq-delayed-message-exchange) plugin for more efficient delayed message handling.

**Advantages:**

- No temporary queue proliferation (reduces queue count by ~98%)
- Lower memory footprint
- Dynamic delay times without creating new queues
- Better scalability for high-volume delayed jobs

**Requirements:**

- RabbitMQ 3.6.0+ (3.8+ recommended)
- `rabbitmq_delayed_message_exchange` plugin installed and enabled

**Plugin Installation:**

```
# For RabbitMQ 3.x
rabbitmq-plugins enable rabbitmq_delayed_message_exchange

# For RabbitMQ 4.x, you may need to download it first:
cd /opt/rabbitmq/plugins
wget https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases/download/v4.1.0/rabbitmq_delayed_message_exchange-4.1.0.ez
rabbitmq-plugins enable rabbitmq_delayed_message_exchange

# Verify the plugin is enabled
rabbitmq-plugins list | grep delayed
```

**Docker Installation:**

```
FROM rabbitmq:4.1-management-alpine

# Download and install the plugin
RUN apk add --no-cache wget && \
    cd /opt/rabbitmq/plugins && \
    wget https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases/download/v4.1.0/rabbitmq_delayed_message_exchange-4.1.0.ez && \
    rabbitmq-plugins enable --offline rabbitmq_delayed_message_exchange && \
    apk del wget
```

**Configuration:**

```
'connections' => [
    'rabbitmq' => [
        // ...

        'options' => [
            'queue' => [
                'delay_strategy' => 'plugin',
                'delayed_exchange' => env('RABBITMQ_DELAYED_EXCHANGE', 'delayed'),
                'delayed_exchange_type' => env('RABBITMQ_DELAYED_EXCHANGE_TYPE', 'direct'),
            ],
        ],
    ],
],
```

**Configuration Options:**

- `delay_strategy`: Either `'dlx'` (default) or `'plugin'`
- `delayed_exchange`: Name of the delayed exchange (default: `'delayed'`)
- `delayed_exchange_type`: Underlying exchange type - `'direct'`, `'topic'`, `'fanout'`, or `'headers'` (default: `'direct'`)

**Environment Variables:**

```
# .env
RABBITMQ_DELAY_STRATEGY=plugin
RABBITMQ_DELAYED_EXCHANGE=delayed
RABBITMQ_DELAYED_EXCHANGE_TYPE=direct
```

#### Usage Examples

[](#usage-examples)

Both strategies use the same Laravel Queue API:

```
use App\Jobs\ProcessPodcast;

// Delay a job by 60 seconds
ProcessPodcast::dispatch($podcast)->delay(now()->addSeconds(60));

// Delay a job by 5 minutes
ProcessPodcast::dispatch($podcast)->delay(now()->addMinutes(5));

// Using laterOn with queue name
Queue::laterOn('rabbitmq', now()->addMinutes(10), new ProcessPodcast($podcast));

// Using later with default queue
Queue::later(now()->addHour(), new ProcessPodcast($podcast));
```

#### Strategy Comparison

[](#strategy-comparison)

FeatureDLX StrategyPlugin Strategy**RabbitMQ Version**All versions3.6.0+ (plugin required)**Setup**Zero configRequires plugin installation**Queue Count**One per unique delay timeOne per job queue**Memory Usage**Higher (more queues)Lower (fewer queues)**Best For**Simple setups, few delay timesHigh volume, varied delays**Fallback**N/AAuto-falls back to DLX if plugin unavailable#### Automatic Fallback

[](#automatic-fallback)

If you configure the `plugin` strategy but the plugin is not installed, the package will **automatically fall back** to the DLX strategy. This ensures your application continues to work even if the plugin is unavailable.

#### Migration from DLX to Plugin

[](#migration-from-dlx-to-plugin)

Migrating is seamless and requires no data migration:

1. Install the plugin on your RabbitMQ server
2. Update your configuration to use `'delay_strategy' => 'plugin'`
3. Deploy the configuration change
4. Old delay queues will auto-expire according to their TTL

No existing delayed jobs are lost, and you can switch back to DLX at any time by changing the configuration.

#### Performance Tips

[](#performance-tips)

For the **plugin strategy**:

- Use `'direct'` exchange type for simple routing
- Use `'topic'` for pattern-based routing of delayed messages
- Monitor the delayed exchange memory usage in RabbitMQ management UI

For the **DLX strategy**:

- Limit the variety of delay times to reduce queue count
- Use `prioritize_delayed` option if you need priority handling

### Horizon support

[](#horizon-support)

Starting with 8.0, this package supports [Laravel Horizon](https://laravel.com/docs/horizon) out of the box. Firstly, install Horizon and then set `RABBITMQ_WORKER` to `horizon`.

Horizon is depending on events dispatched by the worker. These events inform Horizon what was done with the message/job.

This Library supports Horizon, but in the config you have to inform Laravel to use the QueueApi compatible with horizon.

```
'connections' => [
    // ...

    'rabbitmq' => [
        // ...

        /* Set to "horizon" if you wish to use Laravel Horizon. */
       'worker' => env('RABBITMQ_WORKER', 'default'),
    ],

    // ...
],
```

### Use your own RabbitMQJob class

[](#use-your-own-rabbitmqjob-class)

Sometimes you have to work with messages published by another application.
Those messages probably won't respect Laravel's job payload schema. The problem with these messages is that, Laravel workers won't be able to determine the actual job or class to execute.

You can extend the build-in `RabbitMQJob::class` and within the queue connection config, you can define your own class. When you specify a `job` key in the config, with your own class name, every message retrieved from the broker will get wrapped by your own class.

An example for the config:

```
'connections' => [
    // ...

    'rabbitmq' => [
        // ...

        'options' => [
            'queue' => [
                // ...

                'job' => \App\Queue\Jobs\RabbitMQJob::class,
            ],
        ],
    ],

    // ...
],
```

An example of your own job class:

```
