PHP code example of afikrim / laravel-redis-stream

1. Go to this page and download the library: Download afikrim/laravel-redis-stream library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

afikrim / laravel-redis-stream example snippets


$app->register(\Afikrim\LaravelRedisStream\LaravelRedisStreamServiceProvider::class);


...
    'redis' => [
        ...

        'stream' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' => env('REDIS_STREAM_DB', '3'),
            'prefix' => env('REDIS_STREAM_PREFIX', Str::slug(strtolower(config('app.env')) . '.' . strtolower(config('app.name')), '.')) . ':',
        ]
    ],
...



namespace App\Console\Commands;

use Afikrim\LaravelRedisStream\Console\ConsumeCommand as ConsoleConsumeCommand;

class ConsumeCommand extends ConsoleConsumeCommand
{
    /**
     * Function to listen to new event stream
     */
    protected function listen()
    {
        $options = [];
        if ($this->hasOption('group')) {
            $options['group'] = $this->option('group');
        }
        if ($this->hasOption('consumer')) {
            $options['consumer'] = $this->option('consumer');
        }
        if ($this->hasOption('count')) {
            $options['count'] = $this->option('count');
        }
        if ($this->hasOption('block')) {
            $options['block'] = $this->option('block');
        }
        if ($this->laravel->config->get('redis.stream.prefix')) {
            $options['prefix'] = $this->laravel->config->get('redis.stream.prefix');
        }

        $server = new TransporterServer($options);
        $server->addHandler('mystream', function ($result) {return $result;});
        $server->addHandler('mystream2', function ($result) {return $result;});
        // And another awesome handlers...
        $server->listen();
    }
}

protected $commands = [
    // you other command,
    \App\Console\Commands\ConsumeCommad::class
]

...
use Afikrim\LaravelRedisStream\ClientProxy;

...
    $results = ClientProxy::init($options)
        ->publish('mystream2', [
            'name' => 'Aziz',
            'email' => "[email protected]",
        ])
        ->subscribe('mystream2', 60);
...

...
use Afikrim\LaravelRedisStream\ClientProxy;

...
    ClientProxy::init($options)
        ->dispatch('mystream2', [
            'name' => 'Aziz',
            'email' => "[email protected]",
        ]);
...