Skip to content

Instantly share code, notes, and snippets.

@lotharthesavior
Last active September 16, 2022 03:00
Show Gist options
  • Save lotharthesavior/1546c461c153ae3107f3719dfd800211 to your computer and use it in GitHub Desktop.
Save lotharthesavior/1546c461c153ae3107f3719dfd800211 to your computer and use it in GitHub Desktop.
OpenSwoole Redis PubSub Test 1
<?php
Co::set(['hook_flags' => SWOOLE_HOOK_TCP]);
Co\run(function()
{
// refresh
$redis0 = new Redis();
$redis0->connect('127.0.0.1', 6379);
$redis0->flushDB();
$redis0->close();
go(function() {
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
while (true) {
$clientList = $redis->rawCommand('CLIENT', 'LIST');
$clientList = explode(PHP_EOL, $clientList);
array_map(function ($item) {
$item = explode(' ', $item);
echo $item[0] . PHP_EOL;
}, $clientList);
sleep(1);
}
});
go(function() {
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->subscribe(['test'], function (Redis $redis, string $channel, string $payload) {
echo 'Message at channel ' . $channel . ': ' .$payload . PHP_EOL;
});
});
go(function() {
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$counter = 1;
while (true) {
$redis->publish('test', "phpredis " . $counter);
$counter++;
sleep(1);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment