Serksa
All Concepts
Performance & Scaling

Server-Sent Events

1

What is it?

<strong>Server-Sent Events (SSE)</strong> is a technology that allows servers to push updates to clients over a single HTTP connection. It's simpler than WebSockets and perfect for one-way real-time updates.

2

Think of it like...

The News Ticker Analogy

SSE is like a news ticker on TVβ€”the station continuously sends updates, and your TV displays them. You don't send anything back; you just receive.

πŸ“Ί

TV Screen (Client)

Displays updates

πŸ“‘

News Feed (SSE)

Continuous stream

🏒

News Station (Server)

Sends updates

3

Visual Flow

🌐Client

Opens Connection

β†’
πŸ“‘SSE Stream

Server Pushes Data

β†’
πŸ–₯️Server

Sends Events

4

Where you see it

1

Client opens connection

const eventSource = new EventSource('/api/updates')

2

Server keeps connection open

HTTP connection stays alive

3

Server sends events

data: {"message": "New update"}\n\n

4

Client receives events

eventSource.onmessage = (event) => { ... }

5

Auto-reconnect on disconnect

Browser automatically reconnects if connection drops

5

Common Mistake

❌

Wrong

"SSE and WebSockets are the same"

βœ…

Correct

<strong>SSE is simpler, one-way only</strong>. WebSockets are bidirectional but more complex. Use SSE for server-to-client updates (notifications, live feeds). Use WebSockets for chat.

πŸ’‘ Real-World Example

Live stock price updates:

1

Client opens SSE connection to /api/stock-prices

2

Server pushes price updates every second

3

Client displays live prices without polling

4

Connection automatically reconnects if network drops