Example #1
0
<?php

use EventLoop\EventLoop;
use Ramsey\Uuid\UuidInterface;
use Rxnet\Observer\StdOutObserver;
use Rxnet\RabbitMq\RabbitMessage;
require __DIR__ . "/../../vendor/autoload.php";
$loop = EventLoop::getLoop();
$scheduler = new \Rx\Scheduler\EventLoopScheduler($loop);
$rabbit = new \Rxnet\RabbitMq\RabbitMq('rabbit://*****:*****@127.0.0.1:5672/', new \Rxnet\Serializer\Serialize());
// Open connection (lazy way)
$channel = \Rxnet\awaitOnce($rabbit->connect());
$exchange = $rabbit->exchange('amq.direct', [], $channel);
// Every .2s
\Rx\Observable::interval(200)->take(10000)->map(function () {
    return \Ramsey\Uuid\Uuid::uuid4();
})->flatMap(function (UuidInterface $id) use($exchange) {
    return $exchange->produce($id, '/routing/key')->map(function () use($id) {
        return "{$id}\n";
    });
})->subscribe(new StdOutObserver(), $scheduler);
// Open a new channel (lazy way)
$channel = \Rxnet\awaitOnce($rabbit->channel());
$queue = $rabbit->queue('test_queue', [], $channel);
// Say we want to prefetch 1 message at a time
$queue->setQos(1);
// Start one consumer
$queue->consume("Consumer-1")->subscribeCallback(function (RabbitMessage $message) use($scheduler) {
    echo "- consumer 1 consumed : {$message->getData()}\n";
    // Wait 1s to ack
    $scheduler->schedule([$message, 'ack'], 1000);
Example #2
0
<?php

describe("ReactiveX Bunny client", function () {
    it("Connects to bunny", function () {
        $loop = \EventLoop\EventLoop::getLoop();
        $mq = new \Rxnet\RabbitMq\RabbitMq(['host' => '127.0.0.1', 'port' => 5672, 'user' => 'guest', 'password' => 'guest']);
        $cnx = $mq->connect()->retryWhen(function ($errors) {
            return $errors->delay(2000)->doOnNext(function () {
                echo "Disconnected, retry\n";
            });
        });
        \Rxnet\await($cnx);
        $mq->consume("test", 2)->delay(1000, new \Rx\Scheduler\EventLoopScheduler($loop))->subscribeCallback(function (\Rxnet\RabbitMq\RabbitMessage $message) {
            echo "message {$message->getData()}\n";
            $message->ack();
            //var_dump(func_get_args());
        });
        $loop->run();
    });
});