Subscribing to an Update
Subscribing to an Update
~13 Min. Lesezeit Zuletzt aktualisiert am August 8, 2026
WITHOUT React (that comes ONLY in block 9), a Mercure subscription can ALREADY be tested NOW with basic tools – EITHER in the browser via the JavaScript EventSource API OR via curl.
Via EventSource in the browser
const url = new URL('https://localhost/.well-known/mercure');
url.searchParams.append('topic', 'https://localhost/api/projects/1');
const eventSource = new EventSource(url);
eventSource.onmessage = (event) => {
const updatedProject = JSON.parse(event.data);
console.log('Update received:', updatedProject);
};EXACTLY the standard EventSource mechanism, IDENTICAL to ITS use in ANY other web application – Mercure ONLY adds the topic-based subscription model to this standard.
Testing the subscription in the browser console
- Run the JavaScript code above in the browser console (F12 → Console).
- In a SECOND tab/terminal, send a
PATCH /api/projects/1via curl. - The console shows the update IMMEDIATELY, WITHOUT reloading the page.
Watching the raw SSE connection via curl
curl -k -N 'https://localhost/.well-known/mercure?topic=https://localhost/api/projects/1'-N (--no-buffer) is CRITICAL – WITHOUT this flag, curl buffers the response and shows NOTHING until the connection (which DELIBERATELY stays open) eventually gets CLOSED.
event: message
data: {"@id":"/api/projects/1","name":"Updated name", ...}
Achtung: WITHOUT valid authorization (chapter 70), this example only works because mercure: true from chapter 68 means private: true BY DEFAULT – IN FACT, the hub would REJECT the subscription with 403 WITHOUT a token. Chapter 70 adds the missing authorization step.
Subscribing to multiple topics at once
curl -k -N 'https://localhost/.well-known/mercure?topic=https://localhost/api/projects/1&topic=https://localhost/api/projects/2'Multiple topic parameters in the SAME request subscribe to MULTIPLE topics OVER A single connection – ECONOMICAL compared to a separate connection PER topic.
Tipp: Wildcard topics (https://localhost/api/projects/{id}) allow subscribing to ALL projects AT ONCE, instead of listing every id INDIVIDUALLY – USEFUL for an overview page that should react to EVERY update.