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

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

iamfarhad/laravel-rabbitmq
==========================

A robust RabbitMQ driver for Laravel Queue with advanced message queuing, reliable delivery, and high-performance async processing capabilities

1.1.0(6mo ago)3215.6k↓36.3%3[2 issues](https://github.com/iamfarhad/LaravelRabbitMQ/issues)MITPHPPHP ^8.2CI passing

Since Dec 30Pushed 6mo ago2 watchersCompare

[ Source](https://github.com/iamfarhad/LaravelRabbitMQ)[ Packagist](https://packagist.org/packages/iamfarhad/laravel-rabbitmq)[ Docs](https://github.com/iamfarhad/laravel-rabbitmq)[ GitHub Sponsors](https://github.com/iamfarhad)[ RSS](/packages/iamfarhad-laravel-rabbitmq/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (21)Used By (0)

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

[](#laravel-rabbitmq)

[![Latest Stable Version](https://camo.githubusercontent.com/9457cf5f8debe0f93d06a9677aafcc02d3b6a0e1276413b22d215f3bb6b2dcff/68747470733a2f2f706f7365722e707567782e6f72672f69616d6661726861642f6c61726176656c2d7261626269746d712f762f737461626c653f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/iamfarhad/laravel-rabbitmq)[![Total Downloads](https://camo.githubusercontent.com/a99bce6d9288cff1adec2c016cb1edad005eb2d70c5ed0efc23f585a918b93f0/68747470733a2f2f706f7365722e707567782e6f72672f69616d6661726861642f6c61726176656c2d7261626269746d712f646f776e6c6f6164733f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/iamfarhad/laravel-rabbitmq)[![License](https://camo.githubusercontent.com/b14f56c833f6d12ac298e3a36aa2bfaff1d39a79a2cd16442a15c76c18302631/68747470733a2f2f706f7365722e707567782e6f72672f69616d6661726861642f6c61726176656c2d7261626269746d712f6c6963656e73653f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/iamfarhad/laravel-rabbitmq)[![Tests](https://github.com/iamfarhad/LaravelRabbitMQ/actions/workflows/tests.yml/badge.svg)](https://github.com/iamfarhad/LaravelRabbitMQ/actions/workflows/test.yml)

A robust RabbitMQ queue driver for Laravel featuring native Laravel Queue API integration, **advanced connection pooling**, **channel management**, and **high-performance async processing** capabilities.

✨ Key Features
--------------

[](#-key-features)

### Core Features

[](#core-features)

- 🚀 **High-Performance Connection Pooling** - Reuse connections efficiently with configurable limits
- 🔄 **Advanced Channel Management** - Optimal AMQP channel pooling and lifecycle management
- 🛡️ **Resilient Retry Strategy** - Exponential backoff with configurable retry attempts
- 📊 **Real-time Pool Monitoring** - Built-in statistics and health monitoring
- 🔧 **Production Ready** - Comprehensive error handling and graceful shutdowns
- ⚡ **Laravel Native** - Seamless integration with Laravel Queue API
- 🎯 **Zero Configuration** - Works out of the box with sensible defaults

### Advanced Features

[](#advanced-features)

- 🔀 **Advanced Routing** - Topic, fanout, and headers exchanges for complex routing patterns
- 🎯 **Multi-Queue Support** - Configure and manage multiple queues with different settings
- 📈 **Exponential Backoff** - Intelligent retry mechanism with jitter to prevent thundering herd
- 💀 **Dead Letter Exchange** - Automatic handling of failed messages with DLX
- 🔁 **RPC Support** - Synchronous request-response pattern for remote procedure calls
- ✅ **Publisher Confirms** - Reliable message delivery with broker acknowledgments
- 🔒 **Transaction Management** - Atomic operations with commit/rollback support
- ⏰ **Delayed Messages** - Schedule messages for future delivery (TTL or plugin-based)
- 🐌 **Lazy Queues** - Optimize memory usage for high-volume message queues
- ⚖️ **Consumer Priorities** - Control message distribution with priority consumers
- 🔧 **Advanced Queue Options** - Full control over queue arguments and behavior

Table of Contents
-----------------

[](#table-of-contents)

- [Key Features](#-key-features)
- [Requirements](#requirements)
- [Quick Start](#quick-start)
- [Installation](#installation)
- [Configuration](#configuration)
    - [Basic Configuration](#basic-configuration)
    - [Connection Pooling](#connection-pooling)
    - [Environment Variables](#environment-variables)
- [Usage](#usage)
    - [Basic Usage](#basic-usage)
    - [Pool Monitoring](#pool-monitoring)
- [Advanced Features](#advanced-features)
- [Performance Tuning](#performance-tuning)
- [Production Deployment](#production-deployment)
- [Testing](#testing)
- [Troubleshooting](#troubleshooting)
- [Contributing](#contributing)
- [License](#license)

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

[](#requirements)

- PHP 8.2 or higher
- Laravel 11.x or 12.x
- RabbitMQ Server 3.8+
- ext-amqp PHP extension
- ext-pcntl PHP extension (for parallel processing)

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

[](#quick-start)

Get up and running in 5 minutes:

```
# 1. Install package
composer require iamfarhad/laravel-rabbitmq

# 2. Install AMQP extension (if not already installed)
pecl install amqp

# 3. Start RabbitMQ (Docker)
docker run -d --name rabbitmq \
  -p 5672:5672 -p 15672:15672 \
  rabbitmq:3-management

# 4. Configure .env
echo "QUEUE_CONNECTION=rabbitmq" >> .env
echo "RABBITMQ_HOST=localhost" >> .env
echo "RABBITMQ_PORT=5672" >> .env
echo "RABBITMQ_USER=guest" >> .env
echo "RABBITMQ_PASSWORD=guest" >> .env

# 5. Create a job
php artisan make:job ProcessPodcast

# 6. Dispatch the job
php artisan tinker
>>> dispatch(new App\Jobs\ProcessPodcast());

# 7. Start worker
php artisan rabbitmq:consume --queue=default
```

That's it! Your jobs are now processing through RabbitMQ. 🎉

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

[](#installation)

### Install via Composer

[](#install-via-composer)

```
composer require iamfarhad/laravel-rabbitmq
```

### Install AMQP Extension

[](#install-amqp-extension)

The package requires the `ext-amqp` PHP extension. Install it based on your environment:

#### macOS (Homebrew)

[](#macos-homebrew)

```
brew install rabbitmq-c
pecl install amqp
```

#### Ubuntu/Debian

[](#ubuntudebian)

```
sudo apt-get install librabbitmq-dev libssh-dev
sudo pecl install amqp
```

#### Docker

[](#docker)

Add to your Dockerfile:

```
# Install dependencies
RUN apt-get update && apt-get install -y \
    librabbitmq-dev \
    libssh-dev

# Install AMQP extension
RUN pecl install amqp && docker-php-ext-enable amqp
```

#### Verify Installation

[](#verify-installation)

```
php -m | grep amqp
```

You should see `amqp` in the output.

### Publish Configuration

[](#publish-configuration)

```
php artisan vendor:publish --provider="iamfarhad\LaravelRabbitMQ\LaravelRabbitQueueServiceProvider" --tag="config"
```

### Lumen Installation

[](#lumen-installation)

Register the service provider in `bootstrap/app.php`:

```
$app->register(iamfarhad\LaravelRabbitMQ\LaravelRabbitQueueServiceProvider::class);
```

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

[](#configuration)

### Basic Configuration

[](#basic-configuration)

Add the RabbitMQ connection to `config/queue.php`:

```
'connections' => [
    'rabbitmq' => [
        'driver' => 'rabbitmq',
        'queue'  => env('RABBITMQ_QUEUE', 'default'),

        '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', '/'),
            'heartbeat' => env('RABBITMQ_HEARTBEAT_CONNECTION', 0),
        ],

        // 🚀 Connection and Channel Pool Configuration
        'pool' => [
            // Connection Pool Settings
            'max_connections' => env('RABBITMQ_MAX_CONNECTIONS', 10),
            'min_connections' => env('RABBITMQ_MIN_CONNECTIONS', 2),

            // Channel Pool Settings
            'max_channels_per_connection' => env('RABBITMQ_MAX_CHANNELS_PER_CONNECTION', 100),

            // Retry Strategy
            'max_retries' => env('RABBITMQ_MAX_RETRIES', 3),
            'retry_delay' => env('RABBITMQ_RETRY_DELAY', 1000), // milliseconds

            // Health Check Settings
            'health_check_enabled' => env('RABBITMQ_HEALTH_CHECK_ENABLED', true),
            'health_check_interval' => env('RABBITMQ_HEALTH_CHECK_INTERVAL', 30), // seconds
        ],

        'options' => [
            'ssl_options' => [
                'cafile'      => env('RABBITMQ_SSL_CAFILE'),
                'local_cert'  => env('RABBITMQ_SSL_LOCALCERT'),
                'local_key'   => env('RABBITMQ_SSL_LOCALKEY'),
                'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
                'passphrase'  => env('RABBITMQ_SSL_PASSPHRASE'),
            ],
            'queue' => [
                'job' => \iamfarhad\LaravelRabbitMQ\Jobs\RabbitMQJob::class,
                'qos' => [
                    'prefetch_size'  => 0,
                    'prefetch_count' => 10,
                    'global'         => false
                ]
            ],
        ],
    ],
]
```

### Connection Pooling

[](#connection-pooling)

The package features advanced connection and channel pooling for optimal performance:

#### Connection Pool Benefits

[](#connection-pool-benefits)

- **Resource Efficiency**: Reuse existing connections instead of creating new ones
- **Performance**: Significantly faster job processing with reduced connection overhead
- **Reliability**: Automatic health monitoring and dead connection cleanup
- **Scalability**: Configurable limits to handle varying workloads

#### Pool Configuration Options

[](#pool-configuration-options)

SettingDefaultDescription`max_connections`10Maximum number of connections in the pool`min_connections`2Minimum connections to maintain`max_channels_per_connection`100Maximum channels per connection`max_retries`3Connection retry attempts`retry_delay`1000msInitial retry delay (exponential backoff)`health_check_enabled`trueEnable automatic health monitoring`health_check_interval`30sHealth check frequency### Environment Variables

[](#environment-variables)

Add to your `.env` file:

```
# Basic RabbitMQ Configuration
QUEUE_CONNECTION=rabbitmq

# RabbitMQ Connection
RABBITMQ_HOST=127.0.0.1
RABBITMQ_PORT=5672
RABBITMQ_USER=guest
RABBITMQ_PASSWORD=guest
RABBITMQ_VHOST=/
RABBITMQ_QUEUE=default

# Connection Options
RABBITMQ_HEARTBEAT_CONNECTION=0

# 🚀 Connection Pool Configuration
RABBITMQ_MAX_CONNECTIONS=10
RABBITMQ_MIN_CONNECTIONS=2
RABBITMQ_MAX_CHANNELS_PER_CONNECTION=100

# 🛡️ Retry Strategy
RABBITMQ_MAX_RETRIES=3
RABBITMQ_RETRY_DELAY=1000

# 📊 Health Monitoring
RABBITMQ_HEALTH_CHECK_ENABLED=true
RABBITMQ_HEALTH_CHECK_INTERVAL=30

# SSL/TLS Configuration (Optional)
RABBITMQ_SECURE=false
#RABBITMQ_SSL_CAFILE=/path/to/ca.pem
#RABBITMQ_SSL_LOCALCERT=/path/to/cert.pem
#RABBITMQ_SSL_LOCALKEY=/path/to/key.pem
#RABBITMQ_SSL_VERIFY_PEER=true
```

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

#### Job Dispatching

[](#job-dispatching)

```
// Dispatch to default queue
dispatch(new ProcessPodcast($podcast));

// Dispatch to specific queue
dispatch(new ProcessPodcast($podcast))->onQueue('podcasts');

// Delayed dispatch
dispatch(new ProcessPodcast($podcast))->delay(now()->addMinutes(10));
```

### Creating Jobs

[](#creating-jobs)

```
