sajjad385/laravel-rabbitmq-publisher

Multi-connection RabbitMQ publisher for Laravel applications

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/sajjad385/laravel-rabbitmq-publisher

1.0.0 2026-01-14 05:00 UTC

This package is auto-updated.

Last update: 2026-01-15 04:49:09 UTC


README

A lightweight, production-ready RabbitMQ publisher package for Laravel with built-in support for:

  • ✅ Multiple RabbitMQ hosts
  • ✅ Multiple exchanges per host
  • ✅ Runtime exchange & connection selection
  • ✅ Microservice-friendly configuration
  • ✅ Kubernetes / ConfigMap compatibility

This package is ideal for event-driven architectures, microservices, and BO ↔ APP synchronization scenarios.

Features

  • Multi-connection RabbitMQ support
  • Multiple exchanges per connection
  • Supports fanout, direct, topic, headers
  • JSON message publishing
  • Persistent messages
  • Laravel Facade & Dependency Injection support
  • No queue coupling (publisher-only responsibility)

Requirements

  • PHP 7.4+
  • Laravel 8 / 9 / 10
  • RabbitMQ 3.x

Installation

Install via Composer:

composer require sajjad385/laravel-rabbitmq-publisher

Publish the configuration file:

php artisan vendor:publish --tag=config

This will create:

config/rabbitmq-publisher.php

Configuration

Basic Structure

return [

    'default_connection' => 'default',

    'connections' => [
        'default' => [
            'host' => '127.0.0.1',
            'port' => 5672,
            'user' => 'guest',
            'password' => 'guest',
            'vhost' => '/',

            'exchanges' => [
                'default' => [
                    'name' => 'app_exchange',
                    'type' => 'fanout',
                    'durable' => true,
                ],
            ],
        ],
    ],
];

Multiple RabbitMQ Hosts

You can define multiple RabbitMQ brokers (e.g. APP,ADMIN, Partner):

'connections' => [

    'app' => [
        'host' => env('APP_RABBITMQ_HOST'),
        'port' => 5672,
        'user' => env('APP_RABBITMQ_USER'),
        'password' => env('APP_RABBITMQ_PASSWORD'),
        'vhost' => '/',

        'exchanges' => [
            'events' => [
                'name' => 'app_events',
                'type' => 'topic',
                'durable' => true,
            ],
        ],
    ],
],

Environment Variables

Example .env configuration:

# Default
RABBITMQ_HOST=127.0.0.1
RABBITMQ_PORT=5672
RABBITMQ_USER=guest
RABBITMQ_PASSWORD=guest

Usage

Using Facade (Quickest)

use RabbitMQPublisher;

RabbitMQPublisher::publish([
    'event' => 'user.created',
    'user_id' => 1001,
]);

Using Dependency Injection (Recommended)

use Sajjad385\RabbitMQPublisher\Services\Publisher;

class CardService
{
    public function __construct(
        private Publisher $publisher
    ) {}

    public function updateCard()
    {
        $this->publisher->publish(
            payload: [
                'event' => 'card.updated',
                'card_id' => 123,
            ],
            exchange: 'card_changes',
            connection: 'bo'
        );
    }
}

Publishing to a Specific Exchange

RabbitMQPublisher::publish(
    payload: ['status' => 'approved'],
    exchange: 'events',
    connection: 'app'
);

Routing Key Support

Used for direct and topic exchanges.

RabbitMQPublisher::publish(
    payload: ['user_id' => 1],
    exchange: 'events',
    connection: 'app',
    routingKey: 'user.updated'
);

Exchange Types Supported

Type Description
fanout Broadcast to all bound queues
direct Route using exact routing key
topic Pattern-based routing
headers Header-based routing

Example:

'type' => 'topic'

Message Format

All messages are published as:

  • application/json
  • UTF-8 encoded
  • Persistent (delivery_mode = 2)

Example payload:

{
  "event": "card.updated",
  "card_id": 123,
  "timestamp": "2026-01-13T15:30:00Z"
}

Best Practices

✔ Use config only for exchange changes (CR-friendly) ✔ Prefer DI over Facade in services ✔ Keep publisher stateless ✔ Handle retries & DLQ on consumer side ✔ Use fanout for broadcast events

Common Use Cases

  • Microservice event publishing
  • ADMIN → APP data sync
  • Audit / activity streams
  • Replacing Kafka for lightweight systems
  • Event-driven Laravel systems

What This Package Does NOT Do

  • ❌ Consume messages
  • ❌ Manage queues
  • ❌ Retry / DLQ logic
  • ❌ Act as Laravel Queue driver

(These are intentionally left to consumers for clean separation.)

Roadmap (Optional)

  • Async Job wrapper
  • Consumer companion package
  • Retry & DLQ helpers
  • Prometheus metrics
  • TLS / SSL support

License

MIT License © 2026