A Simple EventSource / SSE (Server-Sent Events) client for PHP.
$ composer require eislambey/eventsource
Example: Connect to an endpoint and read 5 messages then close.
<?php
require_once dir(__FILE__) . '/vendor/autoload.php';
use EventSource\Event;
use EventSource\EventSource;
$es = new EventSource("http://example.com");
$messageReceived = 0;
$es->onMessage(function (Event $event) use(&$messageReceived, $es) {
if($es === 4){
$es->abort();
}
$messageReceived++;
echo $event->data, "\n";
});
$es->connect();
Create new instance with the given url.
Set a function that called when a message is received.
Connect to endpoint and receive messages.
Abort the connection and stop receiving messages.
You can set any cURL options (such as cookies and headers) excluding CURLOPT_WRITEFUNCTION
, CURLOPT_NOPROGRESS
, CURLOPT_PROGRESSFUNCTION
.
See: https://www.php.net/manual/tr/function.curl-setopt.php
The MIT License (MIT). Please see LICENSE for more information.