dweik/laravel-redis-stream

Laravel redis stream

1.1.1 2024-10-10 09:25 UTC

This package is auto-updated.

Last update: 2024-12-10 09:52:21 UTC


README

This package using to manage the Redis stream messages listener by firing handler classes based on the defined channels. in this package, you could assign multiple classes to the same channel.

Installation

Require this package with composer using the following command:

composer require dweik/laravel-redis-stream

Laravel

Then you need to publish the config files by executing the following command

php artisan vendor:publish --tag=laravel-redis-stream-config

lumen

You need to add the follow line on bootstrap/app.php file:

$app->register(LaravelStream\Redis\StreamServiceProvider::class);

and make sure that you loaded Redis by add the follow line (if exists no need to add)

$app->register(Illuminate\Redis\RedisServiceProvider::class);

NOTE if you need more information to how setup redis on lumen you can check the official website by a click here

Then you need to copy the streaming config file from vendor/dweik/laravel-redis-stream/config/streaming.php to config/streaming.php

On config/streaming.php file, you need to set up the Redis connection under the redis key. then you need to define the names of the channels by assigning handler classes as below example:

  • note: We prefer use phpredis driver.
    'channels' => [
        'channel-name' => [
            App\Channels\SomeClassChannel::class
        ],
    ],

Also, you could define the trimming value for each channel under trim key. Ex.,

    'trim' => [
        'channel-name' => 1000, // it means keep 1000 messages at most
    ],

Creating handler class

Use artisan command to make a new handler class by using the make:channel-listener function. then you can find it on the app/Channels path

    php artisan make:channel-listener SomeClassChannel

Run channel listener

To run channel listener for all channels use the following command

    php artisan stream:run

To run a specific channel you could pass to the --channel option the channel name like :_

    php artisan stream:run --channel=channel-name

Send a message to Redis stream

There is a facade class that could handle the xADD command on redis

use LaravelStream\Redis\Facades\Stream;

class SomeClass {

    public function(){
    
        /*
        .
        . some code
        .        
        */
        
        
        /**
        *
        * @var string  $channel   channel name
        * @var mixed   $data      the message data
        * @var integer $trim      (default null) the channel messages size,
        *                         when using (null) value, this package will use 
        *                         the defined value on the config file "config/streaming.php".
        *                         when using (zero) value that means stop trimming function
        */
        
        $messageID = Stream::stream( $channel, $data, $trim);
        
        
    
    }

}