Enabling Streaming
To receive a streaming response, set thestream option on the request:
withStreaming():
Consuming the Stream
Once you have a streaming request, callstream() on the pending response. This returns a PHP Generator that yields string chunks:
streamChunkSize setting in HttpClientConfig (default: 256 bytes).
Note: You do not need to explicitly setstream => trueon the request when usingPendingHttpResponse::stream(). The pending response will force streaming mode automatically. However, setting it on the request is useful when middleware needs to know the intended mode before execution.
Streaming LLM Responses
Streaming is essential for AI/LLM integrations where responses are generated token by token. Here is a typical pattern for streaming a chat completion:data: {...}\n\n). To parse these into clean payloads, use the EventSourceMiddleware.
Server-Sent Events with EventSourceMiddleware
TheEventSourceMiddleware handles the SSE protocol for you. It strips the data: prefixes, buffers partial lines, and yields complete event payloads:
data: line and returns the value to yield. Return false to skip an event. This is useful for filtering out [DONE] markers or parsing JSON:
Downloading Large Files
Streaming is the right approach for downloading large files without exhausting memory:Considerations
When working with streaming responses, keep these points in mind:- Memory usage. Streaming avoids buffering the entire response, but be careful not to accumulate chunks in a variable unless you actually need the full content.
- Connection stability. Streaming connections stay open longer and are more sensitive to network interruptions. Pair streaming with retry middleware for resilience.
- Timeouts. The
idleTimeoutsetting inHttpClientConfigcontrols how long the client waits between data packets. Set it to-1to disable idle timeouts for long-lived streams. - Body access. Calling
body()on a streamedHttpResponsethrows aLogicException. Always usestream()for streamed responses. - Middleware order. Middleware that decorates the stream (like
EventSourceMiddleware) should be registered before middleware that reads the final content.