freecoding.school100% FREE · NO SIGNUP
Input HarborISSUE #32 of 46

server-sent events · one-way push

FormaVSCaptain Invalid Input
Forma saysServer-Sent Events stream one-way updates from server to browser over a single long-lived HTTP connection.

When you only need the server to push to the client — live scores, notifications, log tails — Server-Sent Events (SSE) are simpler than WebSockets. The browser opens an EventSource(url); the server keeps the connection open and sends text events; your onmessage fires for each.

SSE auto-reconnects, works over plain HTTP, and is text-only. It's one-directional: for client→server you still use normal requests. Needs a server that streams text/event-stream, so it can't be fully demoed in this offline sandbox.

Power-ups you unlock

Captain Invalid Input attacks — common mistakes

Boss battleSketch the client: open an EventSource, log each message, and handle onerror. (Run it against a real SSE endpoint to see it stream.)

Example code

<!doctype html>
<html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px">
  <pre id="out">SSE needs a streaming server. Client shape:</pre>
  <script>
    const out = document.getElementById('out');
    // against a real text/event-stream endpoint:
    // const es = new EventSource('/events');
    // es.onmessage = e => out.textContent += '\n' + e.data;
    // es.onerror = () => out.textContent += '\n[reconnecting…]';
    out.textContent += '\n\nconst es = new EventSource("/events");\nes.onmessage = e => log(e.data);';
  </script>
</body></html>
▶ Open the interactive comic issue
‹ Web Workers · Background ThreadsWebsockets · Bidirectional Channel ›