Packet capture by WebSocket, after handshake, no more hang-ups

I
opened the Home Assistant page on my phone and placed it on the table. The living room temperature jumped from 26.4 to 26.5, and I didn't touch the screen. This was the thing I was most puzzled about last time after installing <a href="https://www.shunot.com/en/nas/1126.html>HA into the NAS: Isn't the webpage supposed to only move when you click? Why should it move on its own?
Around the same time, I was monitoring download speed in the web version of qBittorrent, and that number was also jumping, jumping one bar every second or two. Both are 'live' pages, but I later realized that these two are implemented completely differently—one relies on repeatedly asking on the webpage, the other on the server proactively pushing it. The latter uses WebSocket.
Two pages that don't refresh—one relies on asking and the other on pushing
qBittorrent's speed numbers use the simplest method: polling. The script on the webpage sends a request to the server every one or two seconds—"How fast is it now?" The server replies '38.2MB/s,' disconnects the connection, and repeats every second. You see, it jumps quite fast, but actually, the webpage is refreshing every second, but each time only a small piece of data is flicked, not the entire page, so it doesn't look like a refresh.
This method was clumsy, but it worked. But the accounts need to be clear: if 100 people open this page at the same time, each asking once per second, the server will handle 100 requests per second, and most of the responses will be "same as before, no change." With more people, most of the server's computing power is spent answering 'no change.'
The temperature of HA is not determined this way. A quick look at packet capture shows that after the page loads, the browser does not initiate a new request during the temperature update—the server proactively pushes down the "26.5" message. There's a premise: the connection must always be open. When the server wants to speak proactively, it must have a ready-made, undisconnected line in hand.
HTTP rules are question-and-answer; servers don't have doors that open proactively
This is the rule of ordinary HTTP. In my article >
Thus, in December 2011, RFC 6455 established WebSocket as the standard. The idea is simple: first use the HTTP gateway to shake hands. Once the handshake is done, the TCP connection won't be dismantled, but replaced with a telephone line that can be opened at both ends at any time. It is not on the same level as the transport layer discussed in the
Handshake: Borrow the HTTP door to enter, and after 101, never look back
Just reading text wasn't enough, so I set up a minimal WebSocket service on my computer and personally ran through it using the most basic socket setup. What the client sends is actually a uniquely designed HTTP request
GET /chat HTTP/1.1 Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: wN4C0c0BNszHE5gU96k5Xw== Sec-WebSocket-Version: 13
The key points are the first two headings: Upgrade: websocket, which literally means "I want to upgrade to websocket"; Connection: Upgrade means "Don't follow the old rules on this connection." The request itself is still in HTTP format, so you go out via port 80 or 443—that's why at home these real-time pages never need to be forwarded via the router, which is different from the port numbers are still the same old ones, and the router treats them as ordinary web traffic.
If the server knows the basics, you can reply:
HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: 7pBSP80/MOKV55KufDiJMIOhP/o=
The most puzzling handshake was between Key and Accept. Sec-WebSocket-Key is a base64 generated by the client with 16 random bytes on site. This time, I caught wN4C0c0BNszHE5gU96k5Xw==, and each connection is different. The server gets it, puts a string of characters written in the protocol at the back of the butt: 258EAFA5-E914-47DA-95CA-C5AB0DC85B11, performs SHA-1 as a whole, then bases 64 to get Accept and returns it to you.
I followed this step and manually calculated it myself, matching the server's Accept by one byte. Note that this is not encryption—the string is public, and anyone can count it. Its purpose is written in the RFC: to prevent old proxy servers on the street from caching upgrade requests as ordinary HTTP responses and causing trouble, and to confirm that the other side truly understands WebSocket, not just a misled ordinary HTTP service. Simply put, it's a code signal, not an anti-eavesdropping device. If you really want to keep it confidential, use wss to install WebSocket into TLS and run it, just like with https.
After connecting, what he said wasn't human speech—it was frames
After the handshake, the data for both hairs on this line is called 'frames.' I had the client send a message saying "Home temperature 26.5" over there, and the server replied after receiving it. Breaking it apart, the first two bytes of the frame are the status bits: FIN (is this the last frame of the entire message) and opcode (what type of the frame is). The commonly used opcodes are just a few examples:
| What is the use of | the opcode | frame type |
|---|---|---|
| 0x1 | text frames | and JSON messages, most commonly |
| 0x2 | binary frames | ,images, audio, and other raw data |
| 0x8 | close the frame | politely and say goodbye, don't just unplug the cable |
| 0x9 | ping | , are you still there |
| 0xA | pong | is there |
There's another interesting rule: every frame sent by the client to the server must be masked—the body text starts with 4 random bytes as keys, and the content is XOR with it byte-byte-byte. The server either restores or restores it when received. Frames sent by the server to the client are not allowed to be masked, effectively exposing the frames. The RFC gives the reason to prevent caching poisoning by middlemans. In my experiment, the phrase "home temperature 26.5" was rewritten by 4 bytes, and the server only recognized it after unlocking it.
ping and pong are the heartbeats that come with the protocol. The troublesome part is that JavaScript in the browser can't send protocol ping at the protocol level; it has to rely on the web page sending a small JSON at the application layer, like {"type":"ping"}, and the other party replies with a single one, every twenty to thirty seconds. Why must it be heartbeat? Read on.
Why does this cable quietly die as soon as the phone locks the screen
?The phone's lock screen is even worse: the system directly cuts off the wireless connection, physically killing the TCP connection. After unlocking, the webpage script detects a heartbeat timeout, reconnects, reshakes hands, and resubscribes—this entire fault tolerance is written by each real-time webpage itself, ignored by the protocol. That's why, when unlocking the phone and returning to the HA page, you occasionally see all the numbers flashing and refreshing once as soon as it reconnects. In the HTTP/3 article, QUIC's connection migration can save network disconnections, but mainstream WebSocket implementations still rely on old TCP. When phones switch from WiFi to traffic, this line is basically broken and reconnected.
Whoever at home uses it, how can you see it with your own eyes
?I took a quick look and realized that quite a few families rely on this line
| What is it used | for in scenarios |
|---|---|
| on HA's mobile page | states that the frontend runs fully via /api/websocket, and when the device status changes, it proactively pushes |
| In the browser, you can view the monitoring screen | , and thedata is pushed all the way through with binary frames. See monitoring Remote troubleshooting real-time link in that post |
| chat and online customer service | Newmessage server pops up directly |
| qBittorrent download page | counterexample: Honest polling, Ask every second |
It's not hard to see it in person; just check the cached article >
To conclude: A Q&A is HTTP's duty, but after 101, talking freely between both ends is WebSocket's privilege. Next time you see numbers on the webpage jumping on their own, press F12 first to tell if it's asking or pushing—the asking one watches the server's mood, the push is the real line that won't hang up.
