Ejemplo n.º 1
0
 public function __construct($loop)
 {
     $redis_host = config('database.redis.default.host');
     $redis_port = config('database.redis.default.port');
     $client = new \Predis\Async\Client('tcp://' . $redis_host . ':' . $redis_port, $loop);
     $client->connect(function ($client) use($loop) {
         // $logger = new \Predis\Async\Client('tcp://127.0.0.1:6379', $loop);
         $logger = null;
         $client->pubSubLoop('WampMessage', function ($event) use($logger) {
             $payload = json_decode($event->payload, true);
             Chat::getInstance()->broadcast($payload['data']);
         });
         Log::v(' ', $loop, "Connected to Redis.");
     });
 }
Ejemplo n.º 2
0
<?php

/*
 * This file is part of the Predis\Async package.
 *
 * (c) Daniele Alessandri <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
require __DIR__ . '/../autoload.php';
$loop = new React\EventLoop\StreamSelectLoop();
$consumer = new Predis\Async\Client('tcp://127.0.0.1:6379', $loop);
$producer = new Predis\Async\Client('tcp://127.0.0.1:6379', $loop);
$consumer->connect(function ($consumer) use($producer) {
    echo "Connected to Redis, will BLPOP for max 10 seconds on `nrk:queue` and produce an item in ~5 seconds.\n";
    $start = microtime(true);
    $consumer->blpop('nrk:queue', 10, function ($response) use($consumer, $producer, $start) {
        list($queue, $stop) = $response;
        $seconds = round((double) $stop - $start, 3);
        echo "Received item from `{$queue}` after {$seconds} seconds!\n";
        $consumer->disconnect();
        $producer->disconnect();
    });
    $consumer->getEventLoop()->addTimer(5, function () use($producer) {
        $producer->lpush('nrk:queue', $microtime = microtime(true), function () use($microtime) {
            echo "Just pushed {$microtime} to `nrk:queue`.\n";
        });
    });
});
$loop->run();
Ejemplo n.º 3
0
<?php

/*
 * This file is part of the Predis\Async package.
 *
 * (c) Daniele Alessandri <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
require __DIR__ . '/../autoload.php';
$client = new Predis\Async\Client('tcp://127.0.0.1:6379');
$client->connect(function ($client) {
    echo "Connected to Redis!\n";
    $tx = $client->transaction();
    $tx->ping();
    $tx->echo("FOO");
    $tx->echo("BAR");
    $tx->execute(function ($replies, $client) {
        var_dump($replies);
        $client->info('cpu', function ($cpuInfo, $client) {
            var_dump($cpuInfo);
            $client->disconnect();
        });
    });
});
$client->getEventLoop()->run();
Ejemplo n.º 4
0
<?php

/*
 * This file is part of the Predis\Async package.
 *
 * (c) Daniele Alessandri <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
require __DIR__ . '/../autoload.php';
$client = new Predis\Async\Client('tcp://127.0.0.1:6379');
$client->connect(function ($client) {
    echo "Connected to Redis!\n";
    $client->set('foo', 'bar', function ($response, $client) {
        echo "`foo` has been set to `bar`, let's check if it's true... ";
        $client->get('foo', function ($foo, $client) {
            echo $foo === 'bar' ? 'YES! :-)' : 'NO :-(', "\n";
            $client->disconnect();
        });
    });
});
$client->getEventLoop()->run();
Ejemplo n.º 5
0
<?php

/*
 * This file is part of the Predis\Async package.
 *
 * (c) Daniele Alessandri <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
require __DIR__ . '/../autoload.php';
$client = new Predis\Async\Client('tcp://127.0.0.1:6379');
$client->connect(function ($client) {
    echo "Connected to Redis, now listening for incoming messages...\n";
    $client->pubsub('nrk:channel', function ($event, $pubsub) {
        $message = "Received message `%s` from channel `%s` [type: %s].\n";
        $feedback = sprintf($message, $event->payload, $event->channel, $event->kind);
        echo $feedback;
        if ($event->payload === 'quit') {
            $pubsub->quit();
        }
    });
});
$client->getEventLoop()->run();
Ejemplo n.º 6
0
 */
require __DIR__ . '/../autoload.php';
class ListPushRandomValue extends Predis\Command\ScriptCommand
{
    const LUA = <<<LUA
math.randomseed(ARGV[1])
local rnd = tostring(math.random())
redis.call('lpush', KEYS[1], rnd)
return rnd
LUA;
    public function getKeysCount()
    {
        return 1;
    }
    public function getScript()
    {
        return self::LUA;
    }
}
$client = new Predis\Async\Client('tcp://127.0.0.1:6379');
$client->getProfile()->defineCommand('lpushrand', 'ListPushRandomValue');
$client->connect(function ($client) {
    echo "Connected to Redis!\n";
    $client->script('load', ListPushRandomValue::LUA, function ($_, $client) {
        $client->lpushrand('random_values', $seed = mt_rand(), function ($value, $client) {
            var_dump($value);
            $client->disconnect();
        });
    });
});
$client->getEventLoop()->run();
Ejemplo n.º 7
0
<?php

require 'vendor/autoload.php';
$client = new Predis\Async\Client('tcp://colin.dev.shazamteam.net:6370');
$client->connect(function ($client) {
    echo "Connected to Redis, now listening for incoming messages...\n";
    $logger = new Predis\Async\Client('tcp://127.0.0.1:6379', $client->getEventLoop());
    $client->pubsub('tags', function ($event) use($logger) {
        $tag = json_decode($event->payload);
        $title = $tag->{'match'}->{'track'}->{'metadata'}->{'trackTitle'};
        $artist = $tag->{'match'}->{'track'}->{'metadata'}->{'artistName'};
        echo "Tagged `{$title}` by {$artist}\n";
    });
});
$client->getEventLoop()->run();
<?php

use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use ChatApp\Chat;
require dirname(__DIR__) . '/vendor/autoload.php';
$chat = new Chat();
$server = IoServer::factory(new HttpServer(new WsServer($chat)), 8080);
$redis = new Predis\Async\Client('tcp://127.0.0.1:6379', $server->loop);
$redis->connect(function ($redis) use($chat) {
    $chat->init($redis);
});
$server->run();