-
Notifications
You must be signed in to change notification settings - Fork 0
/
sse.php
28 lines (25 loc) · 1.1 KB
/
sse.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
<?php
require_once dirname(__FILE__) . '/bootstrap.php';
use Hhxsv5\SSE\Event;
use Hhxsv5\SSE\SSE;
use Hhxsv5\SSE\StopSSEException;
// PHP-FPM SSE Example: push messages to client
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('Connection: keep-alive');
header('X-Accel-Buffering: no'); // Nginx: unbuffered responses suitable for Comet and HTTP streaming applications
$callback = function () {
$id = mt_rand(1, 1000);
$news = [['id' => $id, 'title' => 'title ' . $id, 'content' => 'content ' . $id]]; // Get news from database or service.
if (empty($news)) {
return false; // Return false if no new messages
}
$shouldStop = false; // Stop if something happens or to clear connection, browser will retry
if ($shouldStop) {
throw new StopSSEException();
}
return json_encode(compact('news'));
return ['event' => 'ping', 'data' => 'ping data']; // Custom event temporarily: send ping event
return ['id' => uniqid(), 'data' => json_encode(compact('news'))]; // Custom event Id
};
(new SSE(new Event($callback, 'news')))->start();