-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path3-listen.php
43 lines (29 loc) · 987 Bytes
/
3-listen.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env php
<?php declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use Amp\Postgres\PostgresConfig;
use Amp\Postgres\PostgresConnectionPool;
use function Amp\async;
use function Amp\delay;
$config = PostgresConfig::fromString('host=localhost user=postgres');
$pool = new PostgresConnectionPool($config);
$channel = "test";
$listener = $pool->listen($channel);
printf("Listening on channel '%s'\n", $listener->getChannel());
async(function () use ($pool, $channel, $listener): void {
delay(1);
$pool->notify($channel, "Data 1"); // Send first notification.
delay(1);
$pool->notify($channel, "Data 2"); // Send second notification.
delay(1);
$listener->unlisten();
});
foreach ($listener as $notification) {
printf(
"Received notification from PID %d on channel '%s' with payload: %s\n",
$notification->pid,
$notification->channel,
$notification->payload,
);
}
$pool->close();