1. Go to this page and download the library: Download sinemacula/laravel-sse 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/ */
sinemacula / laravel-sse example snippets
use SineMacula\Sse\EventStream;
use SineMacula\Sse\Emitter;
// maxDuration / maxIterations default to 0 (unbounded). Set a ceiling so the
// stream self-terminates and releases its worker - recommended under Octane.
$stream = new EventStream(heartbeatInterval: 20, maxDuration: 300);
return $stream->toResponse(function (Emitter $emitter): void {
$emitter->emit(['type' => 'tick', 'at' => now()->toIso8601String()], 'update');
}, interval: 1);
use SineMacula\Sse\Concerns\RespondsWithEventStream;
use SineMacula\Sse\Emitter;
class EventController
{
use RespondsWithEventStream;
// Override to bound the stream. Defaults to 0 (unbounded).
protected function maxStreamDuration(): int
{
return 300;
}
public function stream(): \Symfony\Component\HttpFoundation\StreamedResponse
{
return $this->respondWithEventStream(function (Emitter $emitter): void {
$emitter->emit(['type' => 'tick'], 'update');
});
}
}
use SineMacula\Sse\EventStream;
use SineMacula\Sse\Emitter;
use SineMacula\Sse\Enums\StreamTerminationReason;
class MyStream extends EventStream
{
protected function onStreamStart(Emitter $emitter): void
{
$emitter->emit('connected', 'init');
}
protected function onStreamEnd(StreamTerminationReason $reason): void
{
if ($reason === StreamTerminationReason::MAX_DURATION) {
// the configured ceiling was reached; the worker is being released
}
}
protected function shouldContinueAfterError(\Throwable $exception, Emitter $emitter): bool
{
// return true to continue the loop, false to stop
return false;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.