Server-Sent Events (SSE) Complete Guide: One-Way Real-Time Data Streaming
In this tutorial, you'll learn about Server-Sent Events (SSE): a one-way push technology that enables servers to stream data to clients over a single HTTP connection, ideal for real-time notifications and live feeds.
Server-Sent Events (SSE) is a unidirectional push technology enabling servers to stream data to web clients over a single persistent HTTP connection with built-in reconnection.
What You'll Learn
- SSE event stream format and EventSource browser API
- SSE implementation in Express and Python
- Named event types and data-only events
- Auto-reconnection and last-event-id
- SSE vs Websocket tradeoffs and use cases
Why SSE Matters
For use cases where the server needs to push updates to clients but clients don't need to send data back (notifications, stock tickers, log streams), WebSocket is overkill. SSE uses standard HTTP, has built-in browser reconnection, and is simpler to implement. DodaTech's Durga Antivirus Pro uses SSE to push scan result notifications to user dashboards â the browser automatically reconnects if the connection drops, and no client-side WebSocket library is needed.
flowchart LR
A["Browser\n(EventSource API)"] -->|"HTTP GET /events"| B["SSE Server\n(Express / FastAPI)"]
B -->|"data: {\"scan\": \"complete\"}\n\n"| A
B -->|"data: {\"threat\": \"detected\"}\n\n"| A
B -->|"event: error\ndata: ...\n\n"| A
C["EventSource:\nonmessage, onevent"] --> D["Auto-reconnect\non error"]
style B fill:#dbeafe,stroke:#2563eb
style A fill:#fef3c7,stroke:#d97706
Prerequisites: Basic HTTP and JavaScript knowledge. Node.js or Python familiarity for server-side examples.
SSE vs WebSocket
| Feature | SSE | WebSocket |
|---|---|---|
| Direction | Server to client only | Bidirectional |
| Protocol | HTTP | ws:// / wss:// |
| Auto-reconnect | Built-in | Must implement |
| Binary data | No (text only) | Yes |
| Browser API | EventSource |
WebSocket |
| Max concurrent | 6 per domain (HTTP/1.1) | Unlimited |
| Complexity | Low | Medium-High |
Common Mistakes
1. Forgetting the Trailing Newlines
Each SSE event must end with two newlines (\n\n). One newline means more data is coming. Omitting the trailing newline causes the client to wait indefinitely.
2. Not Setting Correct Headers
SSE requires Content-Type: text/event-stream, Cache-Control: no-cache, and Connection: keep-alive. Missing any of these breaks the event stream.
3. Ignoring Last-Event-Id
When a client reconnects, it sends Last-Event-Id header. Use this to resume from the last received event instead of resending all events.
4. No Keepalive Pings
Some proxies drop idle connections after 30-60 seconds. Send periodic comments (: keepalive\n\n) or empty events to keep the connection alive.
5. Using SSE for Bidirectional Communication
SSE is one-way only. If the client needs to send data back, use WebSocket or combine SSE with regular HTTP POST requests.
Practice Questions
- What are the required HTTP headers for SSE?
- How does the browser EventSource API handle reconnection?
- What is the SSE event stream format?
- When should you choose SSE over WebSocket?
- How do you implement named event types in SSE?
Answers:
Content-Type: text/event-stream,Cache-Control: no-cache,Connection: keep-alive.- The browser automatically reconnects when the connection drops. It sends the
Last-Event-Idheader so the server can resume from where it left off. event: type\ndata: payload\n\nâ fields separated by newlines, events terminated by double newline.datais required,event,id, andretryare optional.- SSE is better for one-way updates (notifications, feeds, logs) where server pushes to client. WebSocket is better for bidirectional communication (chat, gaming, collaboration tools).
- Use the
event:field to specify a named event type. The client registers listeners witheventSource.addEventListener('eventName', handler)instead ofonmessage.
Challenge: Build an SSE endpoint for Durga Antivirus Pro that streams real-time scan progress updates. Include named events for scan:started, scan:progress, scan:completed, and scan:error. Implement last-event-id reconnection and an HTML dashboard page that displays scan progress in real time.
FAQ
Try It Yourself
# Test SSE with curl
curl -N -H "Accept: text/event-stream" http://localhost:3000/events
# Output:
# data: {"time": "2026-06-28T12:00:00Z", "value": 42}
#
# data: {"time": "2026-06-28T12:00:02Z", "value": 43}
#
What's Next
| Topic | Description |
|---|---|
| Introduction to SSE | First steps with Server-Sent Events |
| WebSocket Guide | Bidirectional real-time communication |
| Webhooks Guide | Server-to-server event notifications |
| RESTful APIs | Compare with request-response APIs |
Published Topics
All 44 topics in Server-Sent Events (SSE) Complete Guide: One-Way Real-Time Data Streaming are published.