### Install SSE Saloon Plugin Source: https://github.com/hosmelq/sse-saloon/blob/main/README.md Installs the SSE Saloon plugin using Composer. This is the primary method for adding SSE support to your Saloon projects. ```bash composer require hosmelq/sse-saloon ``` -------------------------------- ### Basic SSE Request with Saloon Source: https://github.com/hosmelq/sse-saloon/blob/main/README.md Demonstrates how to add Server-Sent Events support to a Saloon request by using the HasServerSentEvents trait. It defines a GET request to a streaming endpoint and shows how to process incoming events. ```php 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'; } } // Usage: // $response = $connector->send(new StreamNotifications()); // foreach ($response->asEventSource()->events() as $event) { // echo "Data: {$event->data} "; // echo "Event: {$event->event} "; // echo "ID: {$event->id} "; // } ``` -------------------------------- ### Run Tests Source: https://github.com/hosmelq/sse-saloon/blob/main/README.md Executes the project's test suite using Composer. This command is used to verify the functionality and integrity of the SSE Saloon plugin. ```bash composer test ``` -------------------------------- ### Customizing SSE Request Configuration and Headers Source: https://github.com/hosmelq/sse-saloon/blob/main/README.md Shows how to customize the default configuration and headers for a Saloon SSE request. This includes overriding methods like `defaultConfig` and `defaultHeaders` to inject custom settings such as read timeouts and authorization tokens. ```php 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, ]); } } ``` -------------------------------- ### SSE Protocol Exception Handling Source: https://github.com/hosmelq/sse-saloon/blob/main/README.md Illustrates how to handle potential `SSEProtocolException` errors that may occur during Server-Sent Events processing. This involves wrapping the request and event iteration in a try-catch block. ```php use HosmelQ\SSE\SSEProtocolException; try { $response = $connector->send(new EventStreamRequest()); foreach ($response->asEventSource()->events() as $event) { // Process events } } catch (SSEProtocolException $e) { echo 'SSE Error: ' . $e->getMessage(); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.