A Saloon plugin that adds Server-Sent Events support to your HTTP requests. Built on the hosmelq/sse-php library for WHATWG-compliant SSE parsing.
- PHP 8.2+
composer require hosmelq/sse-saloonAdd the HasServerSentEvents trait to your request class:
use HosmelQ\SSE\Saloon\Traits\HasServerSentEvents;
use Saloon\Http\Request;
use Saloon\Enums\Method;
class StreamNotifications extends Request
{
use HasServerSentEvents;
protected Method $method = Method::GET;
public function resolveEndpoint(): string
{
return '/notifications/stream';
}
}Send the request and process events:
$response = $connector->send(new StreamNotifications());
foreach ($response->asEventSource()->events() as $event) {
echo "Data: {$event->data}\n";
echo "Event: {$event->event}\n";
echo "ID: {$event->id}\n";
}Override the default methods to add authentication, custom headers, or configure connection settings:
use HosmelQ\SSE\Saloon\Traits\HasServerSentEvents;
use Saloon\Http\Request;
use Saloon\Enums\Method;
class CustomizedStream extends Request
{
use HasServerSentEvents {
defaultConfig as defaultSSEConfig;
defaultHeaders as defaultSSEHeaders;
}
protected Method $method = Method::GET;
public function __construct(private int $readTimeout, private string $token) {}
public function resolveEndpoint(): string
{
return '/api/stream';
}
protected function defaultConfig(): array
{
return array_merge($this->defaultSSEConfig(), [
'read_timeout' => $this->readTimeout,
]);
}
protected function defaultHeaders(): array
{
return array_merge($this->defaultSSEHeaders(), [
'Authorization' => 'Bearer ' . $this->token,
]);
}
}For detailed information about event processing, parsing, and handling, see the hosmelq/sse-php documentation.
use HosmelQ\SSE\SSEProtocolException;
try {
$response = $connector->send(new EventStreamRequest());
foreach ($response->asEventSource()->events() as $event) {
}
} catch (SSEProtocolException $e) {
echo 'SSE Error: ' . $e->getMessage();
}composer testSee CHANGELOG.md for a list of changes.
The MIT License (MIT). Please see License File for more information.