When the Server Has News
One Connection, Both Directions
Long polling lets the server answer as soon as news arrives, but every answer ends a request. Server-Sent Events let the server keep writing, but only the server. The scoreboard needs both at once: viewers who hear every change the moment it happens, and a scorekeeper who sends scores whenever a question is marked. A
Four things a WebSocket provides
One connection that stays open. A viewer connects once, at the start of the evening, and keeps the same
until the quiz ends. No new request is needed for the next piece of news. Either side may send at any time. This is called full duplex. The server sends a score change the moment it happens, without waiting to be asked, and a client sends whenever it has something to say, without waiting for the server. Neither message is a reply to the other.
Data arrives as whole messages. You know from Networks with Python that TCP is a stream of bytes with no boundaries: two messages sent one after the other may arrive glued together or cut in pieces. So a
each message, much as your length-prefixed protocol in that course put a length in front of every message. The reader gets Owls 12as one message, neverOwand thenls 12. Chapter 3 takes those frames apart byte by byte.It starts as an
request. The client opens a connection to the same as HTTP and sends an ordinary HTTP request first, so anything that already handles HTTP sees a familiar first few bytes. That request asks the server to switch the connection to the WebSocket protocol, and if the server agrees, the same connection carries WebSocket messages from then on. That switch is called the .
Points 1 and 2 are what long polling and Server-Sent Events could not give you together. Point 3 is what makes a long-lived connection usable for messages, and point 4 is how it gets started without anything new between client and server.
What a message costs
Once the connection is open, a message carries very little besides its own bytes. The framing in front of a message the server sends, up to 125 bytes long, is 2 bytes. A message from a client to a server carries 6, because clients add a 4-byte
Compare that with one empty poll from Lesson 2: a request line, a Host header and an If-None-Match header going out, and a status line and an ETag header coming back, all to learn that nothing changed. The request line GET /scores HTTP/1.1 alone is 20 bytes before its line ending, and the frame header of a short message is 2. On a WebSocket nobody has to ask, so a minute with no news sends no requests at all.
A server sends Owls 12 (7 bytes) to a viewer over a WebSocket. How many bytes of framing go with it?
Four techniques side by side
In the picture, each technique gets two time lines, the client above and the server below, with time running right; a diamond is a score change. Follow each diamond to the arrow that carries it to the client.

The table says the same in columns:
| Technique | Who starts each exchange | Direction | Lateness | Cost per message |
|---|---|---|---|---|
| Polling | the client, on a timer | the client asks, the server answers | about half the interval | a full request and response, plus many empty ones |
| Long polling | the client | the client asks, the server answers when there is news | about one trip across the network | a full request and response |
| Server-Sent Events | the client, once; then the server | server to client only | about one trip across the network | a few bytes in the open response |
| WebSocket | the client, once; then either side | both ways on one connection | about one trip across the network | a few bytes of framing |
The table makes the WebSocket look like the winner every time, and it is not. Polling is the simplest of the four, and when news is rare, a few requests an hour cost almost nothing. Server-Sent Events are a good fit when only the server has news. A WebSocket earns its extra machinery when both sides talk often.
Viewers need each score change at once, and the scorekeeper sends changes from the same app. Which fits best?
A status page refreshes a chart once an hour, and nothing is ever sent back to the server. Which fits best?
What does full duplex mean for a WebSocket?
A WebSocket starts as an ordinary HTTP request, and an HTTP request expects one response and then is done. How can that request turn into a connection that never ends? That is the puzzle of Chapter 2, where you write both sides of the opening handshake yourself.