-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
51 lines (38 loc) · 1.54 KB
/
server.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
44
45
46
47
48
49
50
51
#!/usr/bin/env php
<?php
declare(strict_types=1);
use Swoole\HTTP\Server;
use Swoole\HTTP\Request;
use Swoole\HTTP\Response;
// https://www.php.net/manual/ru/function.pg-get-notify.php#121241
$dbConn = new \PDO('pgsql:dbname=sse; host=pgsql;', 'web', 'passwoRt');
$dbConn->exec('LISTEN "channel_name"');
$http = new Server('0.0.0.0', 88);
$http->on('start', static function (Server $server): void {
echo 'Server started'.\PHP_EOL;
});
$http->on('shutdown', static function (Server $server) use ($dbConn): void {
if ($dbConn) {
$dbConn->exec('KILL CONNECTION_ID()');
unset($dbConn);
}
echo 'Server shutdown'.\PHP_EOL;
});
$http->on('request', static function (Request $request, Response $response) use ($dbConn): void {
$response->header('Access-Control-Allow-Origin', '*');
$response->header('Content-Type', 'text/event-stream');
$response->header('Cache-Control', 'no-cache');
$response->header('Connection', 'keep-alive');
$response->header('X-Accel-Buffering', 'no');
$lastEventId = $request->header['last-event-id'] ?? $request->get['lastEventId'] ?? 0;
for ($i = 0; $i < 10; ++$i) {
$result = $dbConn->pgsqlGetNotify(\PDO::FETCH_ASSOC, 10000); // https://www.php.net/manual/en/pdo.pgsqlgetnotify.php
if ($result) {
$response->write("id: $lastEventId\n");
$response->write("data: " . \json_encode($result, \JSON_THROW_ON_ERROR) . "\n\n");
}
//Co::sleep(10); // that or use pgsqlGetNotify timeout?
}
$response->end();
});
$http->start();