@@ -189,30 +189,29 @@ Subscribing to updates in JavaScript is straightforward:
189
189
190
190
.. code-block :: javascript
191
191
192
- < script>
193
192
const es = new EventSource (' http://localhost:3000/hub?topic=' + encodeURIComponent (' http://example.com/books/1' ));
194
193
es .onmessage = e => {
195
194
// Will be called every time an update is published by the server
196
195
console .log (JSON .parse (e .data ));
197
196
}
198
- < / script>
199
197
200
198
Mercure also allows to subscribe to several topics,
201
199
and to use URI Templates as patterns:
202
200
203
201
.. code-block :: javascript
204
202
205
- < script >
206
- const u = new URL (' http://localhost:3000/hub' ); // URL is a built-in JavaScript class to manipulate URLs
203
+ // URL is a built-in JavaScript class to manipulate URLs
204
+ const u = new URL (' http://localhost:3000/hub' );
207
205
u .searchParams .append (' topic' , ' http://example.com/books/1' );
208
- u .searchParams .append (' topic' , ' http://example.com/books/2' ); // Subscribe to updates of several Book resources
209
- u .searchParams .append (' topic' , ' http://example.com/reviews/{id}' ); // All Review resources will match this pattern
206
+ // Subscribe to updates of several Book resources
207
+ u .searchParams .append (' topic' , ' http://example.com/books/2' );
208
+ // All Review resources will match this pattern
209
+ u .searchParams .append (' topic' , ' http://example.com/reviews/{id}' );
210
210
211
211
const es = new EventSource (u);
212
212
es .onmessage = e => {
213
213
console .log (JSON .parse (e .data ));
214
214
}
215
- < / script>
216
215
217
216
.. tip ::
218
217
@@ -222,6 +221,7 @@ and to use URI Templates as patterns:
222
221
.. image :: /_images/mercure/chrome.png
223
222
224
223
To use it:
224
+
225
225
* open the DevTools
226
226
* select the "Network" tab
227
227
* click on the request to the Mercure hub
@@ -293,7 +293,7 @@ by using the ``AbstractController::addLink`` helper method::
293
293
public function __invoke(Request $request): JsonResponse
294
294
{
295
295
// This parameter is automatically created by the MercureBundle
296
- $hubUrl = $this->getParameter('mercure.default_hub');
296
+ $hubUrl = $this->getParameter('mercure.default_hub');
297
297
298
298
// Link: <http://localhost:3000/hub>; rel="mercure"
299
299
$this->addLink($request, new Link('mercure', $hubUrl));
@@ -310,7 +310,6 @@ and to subscribe to it:
310
310
311
311
.. code-block :: javascript
312
312
313
- < script>
314
313
// Fetch the original resource served by the Symfony web API
315
314
fetch (' /books/1' ) // Has Link: <http://localhost:3000/hub>; rel="mercure"
316
315
.then (response => {
@@ -325,7 +324,6 @@ and to subscribe to it:
325
324
const es = new EventSource (h);
326
325
es .onmessage = e => console .log (e .data );
327
326
});
328
- < / script>
329
327
330
328
Authorization
331
329
-------------
0 commit comments