@@ -1146,49 +1146,54 @@ installed in your application::
1146
1146
Consuming Server-Sent Events
1147
1147
----------------------------
1148
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::
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::
1152
1171
1153
1172
use Symfony\Component\HttpClient\EventSourceHttpClient;
1154
1173
1174
+ // the second optional argument is the reconnection time in seconds (default = 10)
1155
1175
$client = new EventSourceHttpClient($client, 10);
1156
- $source = $client->connect('http ://localhost:8080/events');
1176
+ $source = $client->connect('https ://localhost:8080/events');
1157
1177
while ($source) {
1158
1178
foreach ($client->stream($source, 2) as $r => $chunk) {
1159
- // You should handle these chunks yourself
1160
1179
if ($chunk->isTimeout()) {
1161
- dump([
1162
- 'timeout' => [
1163
- 'retry' => 1 + count($r->getInfo('previous_info') ?? [])
1164
- ],
1165
- ]);
1180
+ // ...
1166
1181
continue;
1167
1182
}
1183
+
1168
1184
if ($chunk->isLast()) {
1169
- dump([
1170
- 'eof' => [
1171
- 'retries' => count($r->getInfo('previous_info') ?? [])
1172
- ],
1173
- ]);
1174
- $source = null;
1185
+ // ...
1186
+
1175
1187
return;
1176
1188
}
1177
1189
1178
- // This is a special ServerSentEvent chunk holding the pushed message
1190
+ // this is a special ServerSentEvent chunk holding the pushed message
1179
1191
if ($chunk instanceof ServerSentEvent) {
1180
- dump($chunk);
1192
+ // do something with the server event ...
1181
1193
}
1182
1194
}
1183
1195
}
1184
1196
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
-
1192
1197
Interoperability
1193
1198
----------------
1194
1199
@@ -1465,4 +1470,5 @@ However, using ``MockResponse`` allows simulating chunked responses and timeouts
1465
1470
.. _`libcurl` : https://curl.haxx.se/libcurl/
1466
1471
.. _`amphp/http-client` : https://packagist.org/packages/amphp/http-client
1467
1472
.. _`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
1468
1474
.. _`EventSource` : https://www.w3.org/TR/eventsource/#eventsource
0 commit comments