Mercurial
diff mrjunejune/src/blog/websocket-demystified/index.md @ 132:7a63e41a21fb
[Seobeo] Added debug targets.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Fri, 09 Jan 2026 08:23:54 -0800 |
| parents | b230a743a01e |
| children | 902e29c38d66 |
line wrap: on
line diff
--- a/mrjunejune/src/blog/websocket-demystified/index.md Fri Jan 09 07:42:04 2026 -0800 +++ b/mrjunejune/src/blog/websocket-demystified/index.md Fri Jan 09 08:23:54 2026 -0800 @@ -4,14 +4,20 @@ Today, it’s the standard for everything from chat apps to LLM interfaces, where the model streams bytes back to you one token at a time as it predicts the next word. -Most developers just grab a library like [ws](https://github.com/websockets/ws) for Node.js or [websockets](https://websockets.readthedocs.io/) for Python and call it a day. But many don’t realize the underlying mechanism is actually pretty simple to implement yourself in a day or so. Let's look at how to build it from scratch. +Most developers just grab a library like [ws](https://github.com/websockets/ws) for Node.js or [websockets](https://websockets.readthedocs.io/) for Python and call it a day. But many don’t realize the underlying mechanism is actually pretty simple to implement yourself in a day or so. Also they misunderstand what websocket actually do. Let's look at how to build it from scratch and debunk myths. + +### The "65,535" Myth + +Before we write a single line of code, Let's talk about common myth. You’ll often hear developers say, *"A server can only handle 65,535 WebSocket connections because there are only 65,535 ports."* + +If this were true, Discord or Slack would need millions of separate IP addresses just to function. The confusion comes from the 16-bit size of the TCP port field, but a connection isn't defined by a port alone. You will see in this blog. --- ## Requirements * Ability to type -* Half a brain +* Half a brain to real mechanics, not the surface-level myths. * A computer --- @@ -31,7 +37,8 @@ To start the upgrade from HTTP to WebSocket, the client sends a standard GET request but with some very specific headers. -<div class="center"> <img src="/public/white-noise-grass.png" /> </div> +<div class="center"> <img src="/public/web-socket-header.webp" /> </div> + I’m assuming you know how HTTP works. If not, you can open a developer tool by right clicking on your browser and seeing into network tab and refershign the page. The only interesting values here is the `Sec-WebSocket-Key`. This key is usually a 16-byte random value encoded in **Base64**. @@ -124,13 +131,25 @@ Seobeo_WebSocket_Server_Connection *p_conn = malloc(sizeof(Seobeo_WebSocket_Server_Connection)); memset(p_conn, 0, sizeof(Seobeo_WebSocket_Server_Connection)); -p_conn->p_handle = p_handle; +p_conn->p_handle = p_handle; // file descriptor p_conn->is_active = TRUE; p_conn->fragment_capacity = 4096; p_conn->fragment_buffer = malloc(p_conn->fragment_capacity); ``` +### Wait, what is the p_handle or file descriptor here?? + +The File Descriptor (FD) is the internal ID badge your server assigns to a connection. The OS identifies a unique connection via a 4-tuple; Source IP, Source Port, Destination IP, Destination Port. You can think of the OS as a giant hashmap that links these 4-tuples to an integer (the FD). + +So the limits are from the number of FD and RAM capactiy. You can check your system's FD limit with: + +```bash +ulimit -n +``` + +Now, we have debunked this myth. Let's see how the protocol actually works. + --- ## Frame-Based Protocols