hosmelq / sse-saloon
A Saloon plugin for consuming Server-Sent Events (SSE) streams.
v0.1.0
2025-07-15 14:35 UTC
Requires
- php: ^8.2
- hosmelq/sse: ^0.1.0
- saloonphp/saloon: ^3.14
- thecodingmachine/safe: ^3.3
Requires (Dev)
- ergebnis/composer-normalize: ^2.47
- laravel/pint: ^1.24
- pestphp/pest: ^3.8
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.1
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- rector/rector: ^2.1
- rector/type-perfect: ^2.1
- shipmonk/composer-dependency-analyser: ^1.8
- spaze/phpstan-disallowed-calls: ^4.6
- thecodingmachine/phpstan-safe-rule: ^1.4
- ticketswap/phpstan-error-formatter: ^1.1
- tomasvotruba/type-coverage: ^2.0
README
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.
Requirements
- PHP 8.2+
Installation
composer require hosmelq/sse-saloon
Basic Usage
Add 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"; }
Custom Headers and Configuration
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.
Error Handling
use HosmelQ\SSE\SSEProtocolException; try { $response = $connector->send(new EventStreamRequest()); foreach ($response->asEventSource()->events() as $event) { } } catch (SSEProtocolException $e) { echo 'SSE Error: ' . $e->getMessage(); }
Testing
composer test
Changelog
See CHANGELOG.md for a list of changes.
Credits
License
The MIT License (MIT). Please see License File for more information.