Skip to content

Streams and Server-Sent Events ​

Global helpers:

text
fetch_stream(
    string $url,
    ?array $options = [],
): Fetch\Interfaces\StreamedResponse

fetch_sse(
    string $url,
    ?array $options = [],
): Fetch\Http\EventSource

ClientHandler::stream() accepts:

text
stream(
    string|Method $method = Method::GET,
    string $uri = '',
    array $options = [],
): StreamedResponse

Client::stream() accepts:

text
stream(
    string $url,
    ?array $options = [],
    string|Method $method = Method::GET,
): StreamedResponseInterface

The response exposes stream(int $chunkSize = 8192), lines(), sse(), and buffer(). Reading consumes the underlying body.

EventSource implements IteratorAggregate and yields ServerSentEvent objects. It exposes events(), response(), lastEventId(), and reconnectionTime(). An event has readonly data, type (default message), id, and retry fields; json() decodes its data and isDone() detects the conventional [DONE] payload.

The parser does not reconnect automatically. Streaming I/O is synchronous and uses a separate direct Guzzle path. See the streaming guide for consumption and testing details.

Released under the MIT License.