PHP code example of yoye / redis-broker

1. Go to this page and download the library: Download yoye/redis-broker 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/ */

    

yoye / redis-broker example snippets




// broker.php

t\EventDispatcher\EventDispatcher;
use Yoye\Broker\Adapter\PhpRedisAdapter;
use Yoye\Broker\Broker;
use Yoye\Broker\Event\BrokerEvents;
use Yoye\Broker\Event\MessageEvent;

$client = new Redis();
$client->connect('127.0.0.1', 6379, 0);
$adapter = new PhpRedisAdapter($client);
$dispatcher = new EventDispatcher();
$dispatcher->addListener(BrokerEvents::MESSAGE_RECEIVED, function(MessageEvent $event) {
        $channel = $event->getChannel();
        $message = $event->getMessage();

        var_dump($channel, $message);

        // The event must be marked has done 
        // otherwise the listener will be called indefinitely
        $event->setDone();
});

$broker = new Broker($adapter, ['foo.channel', 'bar.channel'], $dispatcher);
$broker->run();

$client = new Redis();
$client->connect('127.0.0.1', 6379, 0);
$adapter = new PhpRedisAdapter($client);
$dispatcher = new EventDispatcher();
$dispatcher->addListener(BrokerEvents::MESSAGE_RECEIVED, function(MessageEvent $event) {
    var_dump($event->getMessage());
    if ($event->getMessage() === 'FooBar') {
        $event->setDone();
    }
});
$dispatcher->addListener(BrokerEvents::NESTING_LIMIT, function(MessageEvent $event) {
    var_dump('Last call for: ' . $event->getMessage());
});

$broker = new Broker($adapter, ['foo.channel', 'bar.channel'], $dispatcher);
$broker->setNestingLimit(3);
$broker->run();

php broker.php