1. Go to this page and download the library: Download clue/reactphp-eventsource library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$sse = new Clue\React\EventSource\SseDecoder($stream);
$sse = new Clue\React\EventSource\SseDecoder($stream, $lastEventId);
$sse->on('data', function (Clue\React\EventSource\MessageEvent $message) {
$data = json_decode($message->data);
echo "{$data->name} is {$data->age} years old" . PHP_EOL;
});
$sse->on('close', function () use ($sse) {
printf(
'Reconnect in %.1f seconds with last event ID %s' . PHP_EOL,
$sse->lastRetryTime ?? 3.0,
$sse->lastEventId
);
});
$sse->on('error', function (Exception $error) {
// an error occurred, stream will close next
});
$sse->on('end', function () {
// stream successfully ended, stream will close next
});
$sse->on('close', function () {
// stream closed
// possibly after an "end" event or due to an "error" event
});
$sse->close();
$map = new ThroughStream(fn (MessageEvent $message) => $message->data);
$sse->pipe($map)->pipe($logger);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.