freecoding.school100% FREE · NO SIGNUP
Await StationISSUE #15 of 26

server-sent events · EventSource

PromissaVSRace Condition Rex
Promissa saysEventSource opens a one-way Server-Sent Events stream that auto-reconnects.

For server→client updates only (notifications, live feeds), new EventSource(url) is simpler than WebSockets: it streams text events over plain HTTP and reconnects automatically. Listen with onmessage or named-event listeners. It can't send back to the server (use a normal request for that) and needs a text/event-stream endpoint.

Power-ups you unlock

Race Condition Rex attacks — common mistakes

Boss battleSketch an EventSource client that logs each pushed message.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre>const es = new EventSource('/events');
es.onmessage = e => console.log('push:', e.data);
es.onerror   = () => console.log('reconnecting…');
// needs a text/event-stream server endpoint</pre></body></html>
▶ Open the interactive comic issue
‹ Websockets · Client ApiBroadcast Channel · Cross-Tab Messaging ›