Sending the word “hello” from a browser-style client to a server and getting it back cost 292 bytes as an ordinary HTTP request and response in a test run for this post. The same exchange over an open WebSocket cost 18 bytes. That gap is why chat apps, live dashboards and multiplayer games keep a WebSocket open instead of asking the server “anything new?” every second. The protocol, defined in RFC 6455 in December 2011, starts life as an HTTP request and then turns the connection into something else. Following the bytes through a local test server shows how.
Why HTTP alone was awkward
Request, response, done
HTTP is built around a client asking and a server answering. A server cannot speak first. For anything that updates on the server’s schedule, such as a new chat message or a price change, the classic workaround was polling: the client sends a request every few seconds and usually gets back “nothing new.” Every one of those requests carries a full set of headers in both directions.
What WebSockets offer instead
The RFC 6455 abstract describes a protocol that “enables two-way communication between a client running untrusted code” and a remote host that has opted in. After a one-time handshake, either side can send a message at any moment over the same TCP connection, with a header of a few bytes per message.

The upgrade handshake
What the client sends
The handshake is an ordinary HTTP/1.1 GET with a few special headers. The RFC’s own example key was sent to a local server running the Python websockets 17.4 library:
GET /chat HTTP/1.1
Host: 127.0.0.1:8791
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
The whole request was 157 bytes. Upgrade: websocket and Connection: Upgrade ask the server to switch protocols on this connection. The key is a random value, base64-encoded, that the client makes up for each connection.
What the server answers
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
Server: Python/3.13 websockets/17.4
Status 101 means the HTTP conversation is over. From the next byte on, both sides speak WebSocket frames.
Proving the server understood
The accept value is not random. RFC 6455 says the server appends a fixed GUID, 258EAFA5-E914-47DA-95CA-C5AB0DC85B11, to the client’s key, hashes the result with SHA-1 and base64-encodes the hash. Doing that in three lines of Python produced s3pPLMBiTxaQ9kYGzzhZRbK+xOo=, the same value the server returned and the same one printed in the RFC’s example. The check is not security in the password sense. It proves the server actually implements WebSockets, so a plain HTTP server or a confused cache cannot accidentally accept the upgrade.
What a frame looks like
The smallest message
After the handshake, the test client sent “hello” as a single text frame and the server echoed it. The client’s frame was 11 bytes. The server’s reply was 7 bytes, and its raw contents were 81 05 68 65 6c 6c 6f.
The first byte, 0x81, marks a final frame carrying text. The second, 0x05, is the payload length. The last five bytes are the letters h, e, l, l and o. Two bytes of overhead, five bytes of data.
Why the client’s frame is bigger
The client’s frame was four bytes longer because of masking. RFC 6455 states that “a client MUST mask all frames that it sends to the server,” and that servers must not mask theirs. The client picks a random four-byte mask, sends it in the frame header, and XORs the payload with it. Section 10.3 of the RFC explains the reason: with masking, an attacker’s script “does not have control over how the data being sent appears on the wire,” so it cannot craft bytes that a misbehaving proxy in the middle would misread as an HTTP request.
Longer messages
The RFC stores payload lengths from 0 to 125 directly in seven bits. A value of 126 means the next two bytes hold the length, and 127 means the next eight bytes do. A 200-byte test message used the two-byte form: the client’s frame was 208 bytes, the server’s 204. Even at the largest size, the header is at most 14 bytes.

Polling versus a socket, in bytes
The comparison
For the polling side of the test, a minimal Python HTTP server answered GET /poll with the body “hello.” A request carrying the default headers of Python’s requests library came to 149 bytes, and the response, with status line, date, server name, content type and length, came to 143 bytes.
| Method | Client to server | Server to client | Total for “hello” |
|---|---|---|---|
| HTTP poll | 149 bytes | 143 bytes | 292 bytes |
| WebSocket message | 11 bytes | 7 bytes | 18 bytes |
Why real numbers are worse
The test used the leanest headers available. A browser polling a real site sends cookies, a user agent string, accept headers and often authentication tokens, which can push a single request well past a kilobyte. A poll that finds nothing new still pays that full price, while an idle WebSocket costs nothing until something happens. On the other hand, the WebSocket connection itself has to stay open, which ties up a socket and some memory on the server for every connected client.
Keeping the connection alive
Ping and pong
A connection that sits idle can be dropped by routers, proxies or firewalls that clean up quiet connections. RFC 6455 defines ping and pong control frames, opcodes 0x9 and 0xA, for checking that the other side is still there, and says that “All control frames MUST have a payload length of 125 bytes or less.” The Python websockets library does this automatically. Its keepalive documentation says it “sends a Ping frame every 20 seconds” and expects a pong within another 20, after which it treats the connection as broken.
Closing properly
The protocol also defines a closing handshake: each side sends a close frame before dropping TCP. The test client in this post skipped it and simply closed its socket. The server logged ConnectionClosedError: no close frame received or sent, a small reminder that a WebSocket is a conversation with its own manners, not just a raw TCP stream.

Security and the right tool
Check the Origin, use wss
A browser attaches an Origin header to every WebSocket handshake, naming the site whose script opened the connection. RFC 6455 says servers meant only for certain sites “SHOULD verify the |Origin| field is an origin they expect,” and answer an unwanted one with 403 Forbidden. That check stops a script on someone else’s page from quietly opening a connection with the visitor’s cookies attached. It does nothing against a command-line client, which can send any Origin it likes, so authentication still belongs in the application itself.
Encryption is the other half. The RFC says “Connection confidentiality and integrity is provided by running the WebSocket Protocol over TLS (wss URIs),” and that implementations “MUST support TLS and SHOULD employ it.” A page served over HTTPS should open wss:// addresses, never ws://.
When server-sent events fit better
Not every live feed needs two-way traffic. A dashboard that only receives updates can use server-sent events, which Mozilla’s guide to server-sent events describes as “a one-way connection, so you can’t send events from a client to a server.” They run over plain HTTP with the text/event-stream content type, pass through proxies without any upgrade, and reconnect on their own: the guide says “By default, if the connection between the client and server closes, the connection is restarted.” The guide also flags a limit: without HTTP/2, browsers allow only six open connections per browser and domain, a ceiling HTTP/2 raises by carrying many streams on one connection. For one-way updates over HTTP/2, server-sent events are often the simpler choice.
WebSockets behind a reverse proxy
The hop-by-hop problem
In a home lab, the WebSocket server usually sits behind a reverse proxy such as nginx. The nginx WebSocket proxying guide explains the catch: “since the ‘Upgrade’ is a hop-by-hop header, it is not passed from a client to proxied server.” The proxy has to be told to forward it:
location /chat/ {
proxy_pass http://backend;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
Older nginx releases also needed proxy_http_version 1.1; in that block. The nginx change log records that version 1.29.7, released March 24, 2026, made 1.1 the default for proxied connections.
The 60-second surprise
The same guide warns that “By default, the connection will be closed if the proxied server does not transmit any data within 60 seconds,” and that the limit can be raised with proxy_read_timeout. A chat app that works fine during testing and then drops every quiet user after exactly one minute is almost always hitting this. Either raise the timeout or make sure pings travel more often than once a minute. With the websockets library’s default 20-second ping, traffic crosses the proxy three times a minute even when nobody is typing, which keeps the default timeout from ever firing.
The handshake is HTTP, the data is not, and every box in the path has to agree on the switch. When a WebSocket fails, the place to look first is the box in the middle that only understood the first half.
Sources
- IETF, RFC 6455, “The WebSocket Protocol” (December 2011): https://www.rfc-editor.org/rfc/rfc6455.html
- MDN Web Docs, “Using server-sent events”: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
- nginx documentation, “WebSocket proxying”: https://nginx.org/en/docs/http/websocket.html
- websockets documentation, “Keepalive and latency”: https://websockets.readthedocs.io/en/stable/topics/keepalive.html
- nginx, CHANGES (change log): https://nginx.org/en/CHANGES
