Skip to content

[HttpClient] Document EventSourceHttpClient #14032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions http_client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,52 @@ installed in your application::

``CachingHttpClient`` accepts a third argument to set the options of the ``HttpCache``.

Consuming Server-Sent Events
----------------------------

This component provides an `EventSource`_ implementation to consume Server-Sent Events.
Use the :class:`Symfony\\Component\\HttpClient\\EventSourceHttpClient`, open a
connection to a server with the `text/event-stream` content type and consume the stream::

use Symfony\Component\HttpClient\EventSourceHttpClient;

$client = new EventSourceHttpClient($client, 10);
$source = $client->connect('http://localhost:8080/events');
while ($source) {
foreach ($client->stream($source, 2) as $r => $chunk) {
// You should handle these chunks yourself
if ($chunk->isTimeout()) {
dump([
'timeout' => [
'retry' => 1 + count($r->getInfo('previous_info') ?? [])
],
]);
continue;
}
if ($chunk->isLast()) {
dump([
'eof' => [
'retries' => count($r->getInfo('previous_info') ?? [])
],
]);
$source = null;
return;
}

// This is a special ServerSentEvent chunk holding the pushed message
if ($chunk instanceof ServerSentEvent) {
dump($chunk);
}
}
}

The default reconnection time is `10` seconds and is given onto the second argument of
the :class:`Symfony\\Component\\HttpClient\\EventSourceHttpClient`. The method
:method:`Symfony\\Component\\HttpClient\\Response\\AsyncResponse::stream` takes an
optional timeout argument.
The :class:`Symfony\\Component\\HttpClient\\Chunk\\ServerSentEvent` is a special chunk
capable of parsing an event stream as specified by the `EventSource`_ specification.

Interoperability
----------------

Expand Down Expand Up @@ -1419,3 +1465,4 @@ However, using ``MockResponse`` allows simulating chunked responses and timeouts
.. _`libcurl`: https://curl.haxx.se/libcurl/
.. _`amphp/http-client`: https://packagist.org/packages/amphp/http-client
.. _`cURL options`: https://www.php.net/manual/en/function.curl-setopt.php
.. _`EventSource`: https://www.w3.org/TR/eventsource/#eventsource