Skip to content

Commit be32dff

Browse files
committed
minor #14032 [HttpClient] Document EventSourceHttpClient (soyuka)
This PR was merged into the master branch. Discussion ---------- [HttpClient] Document EventSourceHttpClient Documenting symfony/symfony#36692 Commits ------- 1bfa68f Document EventSourceHttpClient
2 parents 062b9c3 + 1bfa68f commit be32dff

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

http_client.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,52 @@ installed in your application::
11431143

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

1146+
Consuming Server-Sent Events
1147+
----------------------------
1148+
1149+
This component provides an `EventSource`_ implementation to consume Server-Sent Events.
1150+
Use the :class:`Symfony\\Component\\HttpClient\\EventSourceHttpClient`, open a
1151+
connection to a server with the `text/event-stream` content type and consume the stream::
1152+
1153+
use Symfony\Component\HttpClient\EventSourceHttpClient;
1154+
1155+
$client = new EventSourceHttpClient($client, 10);
1156+
$source = $client->connect('http://localhost:8080/events');
1157+
while ($source) {
1158+
foreach ($client->stream($source, 2) as $r => $chunk) {
1159+
// You should handle these chunks yourself
1160+
if ($chunk->isTimeout()) {
1161+
dump([
1162+
'timeout' => [
1163+
'retry' => 1 + count($r->getInfo('previous_info') ?? [])
1164+
],
1165+
]);
1166+
continue;
1167+
}
1168+
if ($chunk->isLast()) {
1169+
dump([
1170+
'eof' => [
1171+
'retries' => count($r->getInfo('previous_info') ?? [])
1172+
],
1173+
]);
1174+
$source = null;
1175+
return;
1176+
}
1177+
1178+
// This is a special ServerSentEvent chunk holding the pushed message
1179+
if ($chunk instanceof ServerSentEvent) {
1180+
dump($chunk);
1181+
}
1182+
}
1183+
}
1184+
1185+
The default reconnection time is `10` seconds and is given onto the second argument of
1186+
the :class:`Symfony\\Component\\HttpClient\\EventSourceHttpClient`. The method
1187+
:method:`Symfony\\Component\\HttpClient\\Response\\AsyncResponse::stream` takes an
1188+
optional timeout argument.
1189+
The :class:`Symfony\\Component\\HttpClient\\Chunk\\ServerSentEvent` is a special chunk
1190+
capable of parsing an event stream as specified by the `EventSource`_ specification.
1191+
11461192
Interoperability
11471193
----------------
11481194

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy