PHPackages                             elvenpath/laravel-pub-sub-adapter - 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. elvenpath/laravel-pub-sub-adapter

ActiveLibrary

elvenpath/laravel-pub-sub-adapter
=================================

02PHP

Since Sep 30Pushed 7mo agoCompare

[ Source](https://github.com/elvenpath/laravel-pub-sub-adapter)[ Packagist](https://packagist.org/packages/elvenpath/laravel-pub-sub-adapter)[ RSS](/packages/elvenpath-laravel-pub-sub-adapter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Pub/Sub Messaging Library
=================================

[](#laravel-pubsub-messaging-library)

A robust publish-subscribe messaging system for Laravel applications supporting either Kafka or Redis as the message transport (configured via driver setting).

Features
--------

[](#features)

- **Configurable Transport**: Choose between Kafka or Redis as your message bus driver
- **Attribute-based Configuration**: Simple declaration using PHP 8 attributes
- **Auto-discovery**: Automatic handler and publisher discovery
- **Message Factory**: Automatic serialization/deserialization with complex type support
- **Priority Handlers**: Control handler execution order with priorities
- **Artisan Commands**: Built-in CLI tools for message management
- **PHPStan Max Level**: Full static analysis support
- **Well Tested**: Comprehensive test coverage

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

[](#requirements)

- PHP ^8.2
- Laravel ^10.0 or ^11.0
- ext-json
- ext-kafka (optional - only required when using Kafka driver)
- mateusjunges/laravel-kafka (optional - only required when using Kafka driver)

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

[](#installation)

Install the package via Composer:

```
# For Redis-only usage
composer require elvenpath/laravel-pub-sub-adapter

# For Kafka support, also install:
composer require mateusjunges/laravel-kafka
# And ensure php-kafka extension is installed
```

The service provider will be automatically registered via Laravel's package discovery.

**Note**: If you plan to use Kafka, you'll need to install the `mateusjunges/laravel-kafka` package and ensure the `php-kafka` extension is available. For Redis-only usage, no additional dependencies are required.

### Choosing Your Message Transport

[](#choosing-your-message-transport)

This package supports two message transport options:

- **Redis**: Simple pub/sub messaging, perfect for real-time notifications and lightweight messaging
- **Kafka**: Enterprise-grade streaming platform with durability, partitioning, and replay capabilities

Choose Redis for simpler use cases, or Kafka for high-throughput, mission-critical messaging systems.

#### Redis Driver Requirements

[](#redis-driver-requirements)

For Redis support, you need one of the following:

1. **PhpRedis Extension** (Recommended for production):

    ```
    # Install via PECL
    pecl install redis
    ```
2. **Predis Package** (Pure PHP, no extension required):

    ```
    composer require predis/predis
    ```

Laravel will automatically detect which Redis client is available. Configure your Redis connection in `config/database.php` as per Laravel documentation.

### Publishing Configuration

[](#publishing-configuration)

Publish the configuration file:

```
php artisan vendor:publish --provider="LaravelMessaging\MessagingServiceProvider"
```

This will create a `config/messaging.php` configuration file.

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

[](#configuration)

### Basic Configuration

[](#basic-configuration)

Configure your message bus in `config/messaging.php`:

```
return [
    // Message bus driver: 'kafka' or 'redis'
    'driver' => env('MESSAGING_DRIVER', 'kafka'),

    'service' => [
        'name' => env('MESSAGING_SERVICE_NAME', env('APP_NAME', 'Laravel Messaging')),
        'instance' => env('MESSAGING_SERVICE_INSTANCE', 'default'),
    ],

    // Kafka Configuration
    'kafka' => [],

    // Redis Configuration
    'redis' => [
        'connection' => env('MESSAGING_REDIS_CONNECTION', 'pubsub'),
    ],

    // Handler discovery
    'handler_discovery' => [
        'enabled' => env('MESSAGING_HANDLER_DISCOVERY', true),
        'scan_paths' => [
            app_path('Handlers') => 'App\\Handlers',
            app_path('Messaging/Handlers') => 'App\\Messaging\\Handlers',
        ],
    ],

    // Publisher discovery
    'publisher_discovery' => [
        'enabled' => env('MESSAGING_PUBLISHER_DISCOVERY', true),
        'scan_paths' => [
            app_path('Services') => 'App\\Services',
            app_path('Http/Controllers') => 'App\\Http\\Controllers',
            app_path('Jobs') => 'App\\Jobs',
            app_path('Messaging/Publishers') => 'App\\Messaging\\Publishers',
        ],
    ],

    // Legacy handler subscriptions
    'subscriptions' => [
        // Example:
        // [
        //     'topic' => 'user.events',
        //     'handler' => App\\Handlers\\UserHandler::class,
        //     'priority' => 100,
        // ],
    ],
];
```

### Environment Variables

[](#environment-variables)

Add these to your `.env` file:

```
# Message Bus Configuration
MESSAGING_DRIVER=redis

# Service Configuration
MESSAGING_SERVICE_NAME="My App"
MESSAGING_SERVICE_INSTANCE=default

# Redis Configuration (if using Redis)
MESSAGING_REDIS_CONNECTION=pubsub

# Discovery Configuration
MESSAGING_HANDLER_DISCOVERY=true
MESSAGING_PUBLISHER_DISCOVERY=true
```

Usage
-----

[](#usage)

### Creating Messages

[](#creating-messages)

Messages are simple PHP classes that extend `BaseMessage`:

```
