@@ -189,10 +189,10 @@ Subscribing to updates in JavaScript is straightforward:
189
189
190
190
.. code-block :: javascript
191
191
192
- const es = new EventSource (' http://localhost:3000/hub?topic=' + encodeURIComponent (' http://example.com/books/1' ));
193
- es .onmessage = e => {
192
+ const eventSource = new EventSource (' http://localhost:3000/hub?topic=' + encodeURIComponent (' http://example.com/books/1' ));
193
+ eventSource .onmessage = event => {
194
194
// Will be called every time an update is published by the server
195
- console .log (JSON .parse (e .data ));
195
+ console .log (JSON .parse (event .data ));
196
196
}
197
197
198
198
Mercure also allows to subscribe to several topics,
@@ -201,16 +201,16 @@ and to use URI Templates as patterns:
201
201
.. code-block :: javascript
202
202
203
203
// URL is a built-in JavaScript class to manipulate URLs
204
- const u = new URL (' http://localhost:3000/hub' );
205
- u .searchParams .append (' topic' , ' http://example.com/books/1' );
204
+ const url = new URL (' http://localhost:3000/hub' );
205
+ url .searchParams .append (' topic' , ' http://example.com/books/1' );
206
206
// Subscribe to updates of several Book resources
207
- u .searchParams .append (' topic' , ' http://example.com/books/2' );
207
+ url .searchParams .append (' topic' , ' http://example.com/books/2' );
208
208
// All Review resources will match this pattern
209
- u .searchParams .append (' topic' , ' http://example.com/reviews/{id}' );
209
+ url .searchParams .append (' topic' , ' http://example.com/reviews/{id}' );
210
210
211
- const es = new EventSource (u );
212
- es .onmessage = e => {
213
- console .log (JSON .parse (e .data ));
211
+ const eventSource = new EventSource (url );
212
+ eventSource .onmessage = event => {
213
+ console .log (JSON .parse (event .data ));
214
214
}
215
215
216
216
.. tip ::
@@ -317,12 +317,12 @@ and to subscribe to it:
317
317
const hubUrl = response .headers .get (' Link' ).match (/ <([^ >] + )>;\s + rel=(?:mercure| "[^ "] * mercure[^ "] * ")/ )[1 ];
318
318
319
319
// Append the topic(s) to subscribe as query parameter
320
- const h = new URL (hubUrl);
321
- h .searchParams .append (' topic' , ' http://example.com/books/{id}' );
320
+ const hub = new URL (hubUrl);
321
+ hub .searchParams .append (' topic' , ' http://example.com/books/{id}' );
322
322
323
323
// Subscribe to updates
324
- const es = new EventSource (h );
325
- es .onmessage = e => console .log (e .data );
324
+ const eventSource = new EventSource (hub );
325
+ eventSource .onmessage = event => console .log (event .data );
326
326
});
327
327
328
328
Authorization
0 commit comments