Compare

SSE vs WebSocket

SSEChatGPT streams its answer
New chat

What’s on your mind today?

Explain me the system design of ChatGPT.

Not connected
Message ServiceReady
WebSocketDK and Rajat chat on WhatsApp
3R
Rajatonline

Yesterday

Did you see Virat's cover drive tonight?10:14
Unreal. Timing was 🔥10:15
Nets at 6 tomorrow?10:31
Done. Bring the new bat 🏏10:32
Will do 👍10:34

Today

Dinner at 8?
Not connected
Chat Server AReady

One answer streaming down, or a conversation going both ways. Watch ChatGPT use one and WhatsApp the other.

SSE or WebSocket?

Pick SSE when only the server talks. Pick a WebSocket when both sides talk, often.

DifferenceSSEWebSocket
Who can sendOnly the server, once the stream is openBoth sides, at any time
How it opensAn ordinary HTTP response with Content-Type: text/event-streamAn HTTP request that upgrades: 101 Switching Protocols
Client has more to sayIt makes a new HTTP requestIt writes a frame on the same socket
What travelsText events, one per messageText or binary frames
If the connection dropsThe browser’s EventSource reconnects and sends Last-Event-IDYour code reconnects and catches up
Proxies and CDNsPlain HTTP; turn off response bufferingEvery hop must allow the upgrade and keep idle sockets open
Use it forLLM answers, live scores, notificationsChat, multiplayer games, shared editing

SSE vs WebSocket, explained step by step

1. Open

Two ways a server can keep talking to your phone. On the left, ChatGPT uses SSE, server-sent events, to stream an answer. On the right, WhatsApp uses a WebSocket to carry a chat. Let’s watch how each one opens, talks, and finishes.

First, opening the connection. ChatGPT sends your question as a normal web request.

The server replies, but keeps that reply open. From now on it can keep sending, in one direction only: from the server to your phone.

WhatsApp also starts with a normal web request, with one difference: it asks to upgrade the connection.

The server agrees, and the same connection becomes a WebSocket: a two-way line that stays open.

So, SSE is a one-way stream. A WebSocket is a two-way line.

2. Talk

Now, talking. With SSE, the server sends the answer a few words at a time, and they appear on the phone as they arrive.

Notice the phone sends nothing back on this stream. A follow-up question would be a brand new request.

With the WebSocket, both sides can talk on the same line.

DK sends Rajat a message. It says: Dinner at eight?

The server saves it and answers straight back on the same socket. One grey tick means sent: it’s safely stored on WhatsApp’s servers.

Two grey ticks mean delivered: the message has reached Rajat’s phone, even if he hasn’t opened it yet.

Blue ticks mean read: Rajat has opened the chat.

Then Rajat replies, and it arrives instantly, down the same socket.

DK’s phone sends a read receipt back up. That’s the point of a WebSocket: messages flow both ways, at any time.

3. Finish

Finally, finishing. When the answer is complete, the server sends one last event: done. The phone shows the chat is saved.

Then the response ends and the stream closes. The next question will open a new one.

The WebSocket stays open.

Every so often the server sends a small ping to check the phone is still there, and the phone answers with a pong. The line stays ready for the next message.

In short

So which should you pick? Use SSE when only the server needs to talk, like streaming an AI answer, live scores or notifications. Use a WebSocket when both sides talk often, like chat, multiplayer games or shared documents.