Drivers

The RabbitMQ Driver

Introduction

The RabbitMQ Driver provides a robust integration between your Kipchak application and RabbitMQ message brokers. It handles connection management, SSL/TLS security, and channel allocation for defining multiple queues.

Key features include:

  • Environment-Driven: Fully configurable via environment variables for seamless deployment across dev, stage, and production.
  • Multi-Queue Support: Define and manage multiple queues with dedicated channel isolation.
  • SSL/TLS Support: Built-in configuration for secure connections, including options to skip certificate verification for self-signed certificates.

Installation

Install the driver via composer by running:

composer require kipchak/driver-rabbitmq

Initialise the Driver

Add the following line to your drivers/drivers.php file:

\Mamluk\Kipchak\Driver\RabbitMQ\RabbitMQ::iniitalise($container);

Configuration

The RabbitMQ driver is configured via the kipchak.rabbitmq.php configuration file.

Connection Settings

  • hostname: The RabbitMQ server host (e.g., rabbitmq.example.com or 127.0.0.1).
  • port: The port number (Default: 5672 for non-SSL, 5671 for SSL).
  • username / password: Credentials for authentication.
  • vhost: The Virtual Host to access (Default: /).

Security Settings

  • isSecure (bool): Set to true to enable SSL/TLS encryption.
  • sslVerify (bool): Set to false to disable server certificate validation (useful for self-signed certs in development).

Queue Definitions

You must define the queues your application interacts with in the queues array.

  • name (string): The actual name of the queue in RabbitMQ.
  • channel (int): A unique integer identifier for the channel associated with this queue. This ensures channel isolation per queue.

Example Configuration

<?php

    use function Kipchak\Core\env;

    return [
        'hostname' => env('RABBITMQ_HOST', 'localhost'),
        'port' => env('RABBITMQ_PORT', 5672),
        'username' => env('RABBITMQ_USERNAME', 'guest'),
        'password' => env('RABBITMQ_PASSWORD', 'guest'),
        'vhost' => env('RABBITMQ_VHOST', '/'),
        
        // Security toggles
        'isSecure' => env('RABBITMQ_IS_SECURE', false),
        'sslVerify' => env('RABBITMQ_SSL_VERIFY', false),

        // Queue Mapping
        // Channels are used to ensure queue isolation per queue and must be unique integers
        'queues' => [
            ['name' => 'email_notifications', 'channel' => 1],
            ['name' => 'order_processing',    'channel' => 2],
            ['name' => 'audit_logs',          'channel' => 3]
        ]
    ];

Please see the configuration file in the starter project at https://1x.ax/mamluk/kipchak/starter/~files/master/config/kipchak.rabbitmq.php for full configuration details.

A Note on Queue Channels

When interacting with the driver in your code, the queues configuration ensures that operations on specific queues are routed through their assigned channels. This prevents channel blocking issues where a long-running task on one queue might delay messages on another.

Ensure every queue you intend to use is listed in the queues array with a distinct integer ID.

Usage

use Kipchak\Driver\RabbitMQ\RabbitMQ as RabbitMQDriver;
use PhpAmqpLib\Message\AMQPMessage;

// Publish a message to the queue
$rmq  = RabbitMQDriver::get('queue-name-1');
$message = 'Message published @ ' . date('Y-m-d H:i:s');
$rmq->publish($message);

// Subscribe to a queue
$rmq = RabbitMQDriver::get('queue-name-1');
// The subscribe method is a watched callback that will automatically acknowledge messages as they are processed.
$rmq->subscribe(function(AMQPMessage $message) {
    echo $message->getBody();
    $message->ack();
});

Git Repository

The source code for this driver is available on 1x.ax at https://1x.ax/mamluk/kipchak/drivers/memcached.

Previous
CouchDB
Next
Git