Skip to content

Commit ae3c540

Browse files
committed
Add PubSub examples
1 parent c3017db commit ae3c540

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

examples/publish.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use Clue\React\Redis\Client;
4+
use Clue\React\Redis\Factory;
5+
use Clue\Redis\Protocol\Model\StatusReply;
6+
7+
require __DIR__ . '/../vendor/autoload.php';
8+
9+
$loop = React\EventLoop\Factory::create();
10+
$factory = new Factory($loop);
11+
12+
$channel = isset($argv[1]) ? $argv[1] : 'channel';
13+
$message = isset($argv[2]) ? $argv[2] : 'message';
14+
15+
$factory->createClient()->then(function (Client $client) use ($channel, $message) {
16+
$client->publish($channel, $message)->then(function ($received) {
17+
echo 'successfully published. Received by ' . $received . PHP_EOL;
18+
});
19+
20+
$client->end();
21+
});
22+
23+
$loop->run();

examples/subscribe.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use Clue\React\Redis\Client;
4+
use Clue\React\Redis\Factory;
5+
use Clue\Redis\Protocol\Model\StatusReply;
6+
7+
require __DIR__ . '/../vendor/autoload.php';
8+
9+
$loop = React\EventLoop\Factory::create();
10+
$factory = new Factory($loop);
11+
12+
$channel = isset($argv[1]) ? $argv[1] : 'channel';
13+
14+
$factory->createClient()->then(function (Client $client) use ($channel) {
15+
$client->subscribe($channel)->then(function () {
16+
echo 'Now subscribed to channel ' . PHP_EOL;
17+
});
18+
19+
$client->on('message', function ($channel, $message) {
20+
echo 'Message on ' . $channel . ': ' . $message . PHP_EOL;
21+
});
22+
});
23+
24+
$loop->run();

0 commit comments

Comments
 (0)