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.