itzyanick / sselib
A PHP SSE Lib
1.0.0
2020-10-23 07:25 UTC
Requires
- php: >=5.4
Requires (Dev)
- phpunit/phpunit: ~6.0
This package is auto-updated.
Last update: 2025-07-06 09:46:57 UTC
README
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.
Requirements
- PHP 5.4 or later
Installation via Composer
$ composer require itzyanick/sselib
Usage
Server Code example (PHP)
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);
Start the Server Example
$ php -S 127.0.0.1:9001 -t .
Client Code example (Javascript)
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);