1
0
mirror of https://github.com/danog/postgres.git synced 2024-11-30 04:29:12 +01:00

Add multi-listen example

This commit is contained in:
Aaron Piotrowski 2017-07-29 10:28:20 -05:00
parent a18d7386bf
commit d88d009359
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB

63
examples/multi-listen.php Normal file
View File

@ -0,0 +1,63 @@
#!/usr/bin/env php
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use Amp\Iterator;
use Amp\Loop;
use Amp\Postgres;
Loop::run(function () {
$pool = Postgres\pool('host=localhost user=postgres');
$channel1 = "test1";
$channel2 = "test2";
/** @var \Amp\Postgres\Listener $listener1 */
$listener1 = yield $pool->listen($channel1);
printf("Listening on channel '%s'\n", $listener1->getChannel());
/** @var \Amp\Postgres\Listener $listener2 */
$listener2 = yield $pool->listen($channel2);
printf("Listening on channel '%s'\n", $listener2->getChannel());
Loop::delay(6000, function () use ($listener1) { // Unlisten in 6 seconds.
printf("Unlistening from channel '%s'\n", $listener1->getChannel());
return $listener1->unlisten();
});
Loop::delay(4000, function () use ($listener2) { // Unlisten in 4 seconds.
printf("Unlistening from channel '%s'\n", $listener2->getChannel());
return $listener2->unlisten();
});
Loop::delay(1000, function () use ($pool, $channel1) {
return $pool->notify($channel1, "Data 1.1");
});
Loop::delay(2000, function () use ($pool, $channel2) {
return $pool->notify($channel2, "Data 2.1");
});
Loop::delay(3000, function () use ($pool, $channel2) {
return $pool->notify($channel2, "Data 2.2");
});
Loop::delay(5000, function () use ($pool, $channel1) {
return $pool->notify($channel1, "Data 1.2");
});
$iterator = Iterator\merge([$listener1, $listener2]); // Merge both listeners into single iterator.
while (yield $iterator->advance()) {
$notification = $iterator->getCurrent();
printf(
"Received notification from PID %d on channel '%s' with payload: %s\n",
$notification->pid,
$notification->channel,
$notification->payload
);
}
});