PHPackages                             ody/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. [Framework](/categories/framework)
4. /
5. ody/amqp

ActiveLibrary[Framework](/categories/framework)

ody/amqp
========

AMQP module for ODY framework

0.2.0(1y ago)02MITPHPPHP &gt;=8.3

Since Mar 27Pushed 1y ago1 watchersCompare

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

READMEChangelog (3)Dependencies (3)Versions (5)Used By (0)

RabbitMQ Integration for ODY Framework
======================================

[](#rabbitmq-integration-for-ody-framework)

This package provides RabbitMQ integration for the ODY framework, allowing you to easily implement asynchronous messaging patterns in your application.

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

[](#installation)

```
composer require ody/amqp
```

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

[](#configuration)

First, publish the configuration file:

```
php ody publish:config ody/amqp
```

This will create a `config/amqp.php` file where you can configure your RabbitMQ connections:

```
return [
    'enable' => true,

    'default' => [
        'host' => env('RABBITMQ_HOST', 'localhost'),
        'port' => env('RABBITMQ_PORT', 5672),
        'user' => env('RABBITMQ_USER', 'guest'),
        'password' => env('RABBITMQ_PASSWORD', 'guest'),
        'vhost' => env('RABBITMQ_VHOST', '/'),

        'concurrent' => [
            'limit' => 10,  // Max concurrent consumers per process
        ],

        'params' => [
            'connection_timeout' => 3.0,
            'read_write_timeout' => 3.0,
            'heartbeat' => 60,
            'keepalive' => true,
        ],
    ],

    // You can define multiple connection pools
    'analytics' => [
        'host' => 'analytics-rabbitmq',
        // Other connection settings
    ],

    // Connection pooling configuration
    'pool' => [
        'enable' => true,
        'max_connections' => 20,
        'max_channels_per_connection' => 20,
        'max_idle_time' => 60,  // seconds
    ],

    'producer' => [
        'paths' => ['app/Producers'],
        'retry' => [
            'max_attempts' => 3,
            'initial_interval' => 1000,  // ms
            'multiplier' => 2.0,
            'max_interval' => 10000,  // ms
        ],
    ],

    'consumer' => [
        'paths' => ['app/Consumers'],
        'prefetch_count' => 10,
        'auto_declare' => true,
    ],

    'process' => [
        'enable' => true,
        'max_consumers' => 10,
        'auto_restart' => true,
    ]
];
```

Setting Up Docker
-----------------

[](#setting-up-docker)

For local development, you can use Docker to run RabbitMQ:

```
docker run -d --name rabbitmq \
  -p 5672:5672 \
  -p 15672:15672 \
  -e RABBITMQ_DEFAULT_USER=admin \
  -e RABBITMQ_DEFAULT_PASS=password \
  rabbitmq:3-management
```

Or use docker-compose:

```
version: '3.8'

services:
  rabbitmq:
    image: rabbitmq:3-management
    container_name: rabbitmq
    ports:
      - "5672:5672"   # AMQP protocol port
      - "15672:15672" # Management UI port
    environment:
      - RABBITMQ_DEFAULT_USER=admin
      - RABBITMQ_DEFAULT_PASS=password
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq
      - rabbitmq_log:/var/log/rabbitmq

volumes:
  rabbitmq_data:
  rabbitmq_log:
```

Connection Pooling
------------------

[](#connection-pooling)

The framework implements connection pooling to optimize RabbitMQ connections by reusing existing connections and channels instead of creating new ones for each operation. This significantly improves performance in high-throughput scenarios.

### Pool Configuration

[](#pool-configuration)

Configure the connection pool in your `config/amqp.php` file:

```
'pool' => [
    'enable' => true,                   // Enable/disable connection pooling
    'max_connections' => 20,            // Maximum connections in the pool
    'max_channels_per_connection' => 20,// Maximum channels per connection
    'max_idle_time' => 60,              // Maximum idle time in seconds
],
```

### Usage

[](#usage)

Connection pooling works transparently with the existing AMQP API:

```
// The publish method automatically uses pooled connections
AMQP::publish(UserCreatedProducer::class, [
    $user->id,
    $user->email,
    $user->username
]);

// For advanced usage, you can directly access pooled resources
$connection = AMQP::getPooledConnection();
$channel = AMQP::getPooledChannel();
```

Creating Producers
------------------

[](#creating-producers)

Producers send messages to RabbitMQ. Create a producer class in your `app/Producers` directory:

```
