A library to create real-time server-sent events on your own PHP Server. On the client side you can use a simple and easy to use javascript to get the events in real-time.
- PHP 5.4 or later
$ composer require itzyanick/sselib
use ItzYanick\SSELIB\Server;
use ItzYanick\SSELIB\Event;
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('Connection: keep-alive');
header('X-Accel-Buffering: no'); // optional
$dataCallback = function ($var, $prevData) {
$push = ['content' => 'Fools'];
if (empty($push)) {
return false;
}
return [json_encode(compact('push')), json_encode(compact('push'))];
};
$abortCallback = function () {
};
$server = new Server();
$server->setEvent(new Event($dataCallback, 'data', 'var', $abortCallback));
$server->startServer(0);
$ php -S 127.0.0.1:9001 -t .
const source = new EventSource('http://127.0.0.1:9001/sse.php', {withCredentials:true});
source.addEventListener('data', function(event) {
console.log(event.data);
// source.close(); // disconnect stream
}, false);