8000 Rewords and tweaks · symfony/symfony-docs@da7c12b · GitHub
[go: up one dir, main page]

Skip to content

Commit da7c12b

Browse files
committed
Rewords and tweaks
1 parent be32dff commit da7c12b

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

http_client.rst

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,49 +1146,54 @@ installed in your application::
11461146
Consuming Server-Sent Events
11471147
----------------------------
11481148

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::
1149+
.. versionadded:: 5.2
1150+
1151+
The feature to consume server-sent events was introduced in Symfony 5.2.
1152+
1153+
`Server-sent events`_ is an Internet standard used to push data to web pages.
1154+
Its JavaScript API is built around an `EventSource`_ object, which listens to
1155+
the events sent from some URL. The events are a stream of data (served with the
1156+
``text/event-stream`` MIME type) with the following format:
1157+
1158+
.. code-block:: text
1159+
1160+
data: This is the first message.
1161+
1162+
data: This is the second message, it
1163+
data: has two lines.
1164+
1165+
data: This is the third message.
1166+
1167+
Symfony's HTTP client provides an EventSource implementation to consume these
1168+
server-sent events. Use the :class:`Symfony\\Component\\HttpClient\\EventSourceHttpClient`
1169+
to wrap your HTTP client, open a connection to a server that responds with a
1170+
``text/event-stream`` content type and consume the stream as follows::
11521171

11531172
use Symfony\Component\HttpClient\EventSourceHttpClient;
11541173

1174+
// the second optional argument is the reconnection time in seconds (default = 10)
11551175
$client = new EventSourceHttpClient($client, 10);
1156-
$source = $client->connect('http://localhost:8080/events');
1176+
$source = $client->connect('https://localhost:8080/events');
11571177
while ($source) {
11581178
foreach ($client->stream($source, 2) as $r => $chunk) {
1159-
// You should handle these chunks yourself
11601179
if ($chunk->isTimeout()) {
1161-
dump([
1162-
'timeout' => [
1163-
'retry' => 1 + count($r->getInfo('previous_info') ?? [])
1164-
],
1165-
]);
1180+
// ...
11661181
continue;
11671182
}
1183+
11681184
if ($chunk->isLast()) {
1169-
dump([
1170-
'eof' => [
1171-
'retries' => count($r->getInfo('previous_info') ?? [])
1172-
],
1173-
]);
1174-
$source = null;
1185+
// ...
1186+
11751187
return;
11761188
}
11771189

1178-
// This is a special ServerSentEvent chunk holding the pushed message
1190+
// this is a special ServerSentEvent chunk holding the pushed message
11791191
if ($chunk instanceof ServerSentEvent) {
1180-
dump($chunk);
1192+
// do something with the server event ...
11811193
}
11821194
}
11831195
}
11841196

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-
11921197
Interoperability
11931198
----------------
11941199

@@ -1465,4 +1470,5 @@ However, using ``MockResponse`` allows simulating chunked responses and timeouts
14651470
.. _`libcurl`: https://curl.haxx.se/libcurl/
14661471
.. _`amphp/http-client`: https://packagist.org/packages/amphp/http-client
14671472
.. _`cURL options`: https://www.php.net/manual/en/function.curl-setopt.php
1473+
.. _`Server-sent events`: https://html.spec.whatwg.org/multipage/server-sent-events.html
14681474
.. _`EventSource`: https://www.w3.org/TR/eventsource/#eventsource

0 commit comments

Comments
 (0)
0