WebSocket ============ .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow **What is WebSocket?** WebSocket is a communication protocol that provides full-duplex, bidirectional communication channels over a single TCP connection. It is designed to allow real-time interaction between a client (like a browser) and a server with minimal overhead. .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow **Why is WebSocket useful?** * Real-time communication – Enables low-latency, two-way data exchange * Reduced overhead – Unlike HTTP, headers are sent only once during the initial handshake * Persistent connection – Keeps a single connection open for ongoing interaction * Ideal for live updates – Great for chat, gaming, and real-time dashboards .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow **How it works?** * **Handshake** – Starts with an HTTP request to establish a WebSocket connection * **Upgrade** – If accepted, the protocol upgrades to WebSocket * **Open connection** – A persistent connection remains open * **Data exchange** – Messages can be sent in both directions at any time * **Close** – Either side can close the connection when done .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow **Where is WebSocket used?** * Online gaming – For real-time multiplayer synchronization * Chat applications – Enables instant messaging * Live sports and stock tickers – For pushing real-time data updates * Collaborative tools – Like whiteboards or shared editing platforms .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow **Which OSI layer does WebSocket belong to?** * WebSocket defines message formats and communication logic for applications * It builds on top of TCP (via HTTP) but operates at the application level * It belongs to the **Application Layer (Layer 7)** of the OSI model. .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow **What are the differences in fields between normal HTTP packets and WebSocket HTTP packets?** * Normal HTTP packets contain standard request/response lines and headers like `Host`, `User-Agent`, `Accept`, `Content-Type`, `Content-Length`, and optionally `Connection: keep-alive` * WebSocket HTTP packets start as a normal HTTP GET request but include special headers to initiate the WebSocket handshake: - `Upgrade: websocket` - `Connection: Upgrade` - `Sec-WebSocket-Key` - `Sec-WebSocket-Version` - Optional headers like `Origin`, `Sec-WebSocket-Protocol` * Normal HTTP packets may have a body depending on the method, but WebSocket handshake requests do not contain a body * The presence of `Upgrade` and `Connection: Upgrade` headers along with `Sec-WebSocket-Key` distinguishes WebSocket handshake packets from normal HTTP packets * After the handshake, WebSocket communication no longer uses HTTP packets but switches to WebSocket frames .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow **Can the WebSocket protocol work without HTTP packets?** * No, the WebSocket protocol **requires HTTP** for its initial handshake phase * The connection begins with an HTTP GET request that includes special headers to request an upgrade to the WebSocket protocol * The server responds with an HTTP 101 Switching Protocols response if it agrees to upgrade * This HTTP-based handshake ensures compatibility with existing web infrastructure (proxies, firewalls, ports) * After the handshake, HTTP is no longer used — communication switches to raw WebSocket frames over TCP * Without the HTTP handshake, the WebSocket protocol **cannot be initiated** .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow **What is the main difference between HTTP and WebSocket?** * HTTP is a **stateless, request-response** protocol — the client sends a request, and the server responds * WebSocket is a **stateful, full-duplex** protocol — both client and server can send messages at any time * HTTP closes the connection after each transaction (unless keep-alive is used), while WebSocket maintains a **persistent connection** * WebSocket begins with an HTTP handshake, but once established, it **switches to its own protocol** over the same TCP connection * WebSocket is ideal for **real-time communication**, such as chats, live updates, and games, unlike HTTP which is suited for standard web requests .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow **Is WebSocket supported securely like HTTPS?** * Yes, WebSocket supports a secure version called **WSS (WebSocket Secure)**, similar to HTTPS * WSS runs over **TLS/SSL** encryption, providing confidentiality and data integrity * Secure WebSocket connections use the URL scheme `wss://` instead of `ws://` * Like HTTPS, WSS ensures that data sent between client and server is encrypted and protected from eavesdropping or tampering * Using WSS is recommended especially for sensitive or production environments to maintain security .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow **Is WebSocket Windows specific?** * No, WebSocket is not Windows-specific. * WebSocket is a protocol that is platform-independent and can be implemented on any operating system including Windows, Linux, macOS, and mobile platforms. * It is supported by most modern web browsers, regardless of the operating system. .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow **Is WebSocket Linux specific?** * No, WebSocket is not Linux-specific. * WebSocket is supported on a wide range of platforms, including Windows, Linux, macOS, and mobile operating systems. * It is used for real-time, bidirectional communication over web browsers or applications on all these platforms. .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow **Which Transport Protocol is used by WebSocket?** * WebSocket uses **TCP** (Transmission Control Protocol) as its transport protocol. * It establishes a persistent, full-duplex communication channel over TCP, typically starting with an HTTP or HTTPS handshake. .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow **Which Port is used by WebSocket?** * WebSocket typically uses **TCP port 80** for unsecured connections (ws://). * For secure WebSocket connections (wss://), it uses **TCP port 443**, the same port as HTTPS. .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow **Is WebSocket using Client-server model?** * Yes, WebSocket uses the client-server model. * The client (typically a web browser) initiates the WebSocket handshake with the server. * Once the handshake is completed, the client and server can exchange messages in both directions over the open connection. .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Learnings in this section ` * :ref:`Terminology ` * :ref:`Version Info ` * :ref:`WebSocket Version&RFC Details ` * :ref:`WebSocket Basic Setup on Ubuntu using IPv4 ` * :ref:`WebSocket Basic Setup on Ubuntu using IPv6 ` * :ref:`WebSocket Protocol Packet Details ` * :ref:`WebSocket Usecases ` * :ref:`WebSocket Basic Features ` * :ref:`WebSocket Feature : Full Duplex Communication ` * :ref:`WebSocket Feature : Persistent Connection ` * :ref:`WebSocket Feature : Low Latency ` * :ref:`WebSocket Feature : Light weight Protocol ` * :ref:`WebSocket Feature : Real Time Data Transfer ` * :ref:`WebSocket Feature : Cross Platform Support ` * :ref:`WebSocket Feature : Firewall Friendly ` * :ref:`WebSocket Feature : Binary and Text Support ` * :ref:`WebSocket Feature : Built in Ping Pong Frames ` * :ref:`WebSocket Feature : Protocol Upgrade from HTTP ` * :ref:`Reference links ` .. _WebSocket_step1: .. tab-set:: .. tab-item:: Learnings in this section * In this section, you are going to learn .. _WebSocket_step2: .. tab-set:: .. tab-item:: Terminology * Terminology .. _WebSocket_step3: .. tab-set:: .. tab-item:: Version Info * Version Info .. _WebSocket_step5: .. tab-set:: .. tab-item:: WebSocket Version&RFC Details .. csv-table:: :file: ./WebSocket/WebSocket_rfcdetails.csv :widths: 10,10,10,30 :header-rows: 1 .. _WebSocket_step19: .. tab-set:: .. tab-item:: WebSocket Basic Setup on Ubuntu using IPv4 **Installation & Setup Steps** * Step-1: Install websocketd on the server .. code-block:: shell server:~$sudo apt update server:~$sudo apt install websocketd * Step-2: Create the echo_and_log.sh script on the server .. code-block:: shell server:~$nano echo_and_log.sh * Paste the following content: .. code-block:: shell #!/bin/bash while true; do read input if [ -n "$input" ]; then # only log if the input is not empty echo "$(date) - Received: $input" >> /tmp/websocketd_log.txt echo "$input" # Echo the message back to the client fi done * Save and exit. * Step-3: Make the script executable .. code-block:: shell server:~$chmod +x echo_and_log.sh * Step-4: Open firewall port 8765 (if firewall is enabled) Check firewall status: .. code-block:: shell server:~$sudo ufw status If enabled, allow port 8765: .. code-block:: shell server:~$sudo ufw allow 8765 server:~$sudo ufw enable # If it wasn't enabled before * Step-5: Start the WebSocket server .. code-block:: shell server:~$websocketd --port=8765 --binary ./echo_and_log.sh * You should see logs like: .. code-block:: shell INFO | server | | Serving using application : ./echo_and_log.sh INFO | server | | Starting WebSocket server : ws://:8765/ * Step-6: Install websocat on the client machine .. code-block:: shell client:~$sudo apt install curl client:~$curl -LO https://github.com/vi/websocat/releases/download/v1.7.0/websocat_amd64-linux client:~$chmod +x websocat_amd64-linux client:~$sudo mv websocat_amd64-linux /usr/local/bin/websocat * Step-7: Connect client to server via websocat .. code-block:: shell client:~$websocat ws://192.168.0.128:8765 .. note :: * Replace ``192.168.0.128`` with your server IP. **TESTCASE-1: Successful Echo Message** * To test WebSocket communication between a Linux server using `websocketd` and a client using `websocat`. .. note:: - Ensure both client and server are on the same interface. - Make sure port `8765` is open and not blocked by firewall. - Use the correct server IP address (e.g., `192.168.0.128`). * Step-1 : Start WebSocket server .. code-block:: shell server:~$ websocketd --port=8765 --binary ./echo_and_log.sh * Step-2 : Connect to the server using `websocat` .. code-block:: shell client:~$ websocat ws://192.168.0.128:8765 .. note:: - Replace `192.168.0.128` with the actual IP address of your WebSocket server. - Ensure the client machine can reach this IP and port (8765) over the network. * Step-3 : Send test message from client .. code-block:: shell HELLO FROM CLIENT .. note:: - This message will be echoed back to the client. - It will also be logged in `/tmp/websocketd_log.txt` on the server. * Step-4 : Send test message from client and observe echo .. code-block:: shell HELLO FROM CLIENT <-- (User typed message) HELLO FROM CLIENT <-- (Server echo response) .. note:: - The first line (`HELLO FROM CLIENT`) is the message you type and send. - The second line (`HELLO FROM CLIENT`) is the echo message sent back from the WebSocket server. * Step-5 : View server log to confirm received messages .. code-block:: shell server:~$ tail -f /tmp/websocketd_log.txt Wed Aug 6 12:15:42 UTC 2025: Received: HELLO FROM CLIENT .. note:: - This command continuously shows new log entries. - You should see entries like: * Step-6 : Wireshark capture :download:`Download Wireshark capture ` **TESTCASE-2: Multiple Message Echo in Single Session** * To validate WebSocket echo functionality when sending multiple messages from a client in one continuous connection. .. note:: - This test ensures that the WebSocket server maintains persistent session state. - All messages sent should be echoed back individually. - The server log should record each message with a timestamp. * Step-1 : Ensure the server is running .. code-block:: shell server:~$ websocketd --port=8765 --binary ./echo_and_log.sh * Step-2 : Connect to the server using `websocat` from the client .. code-block:: shell client:~$ websocat ws://192.168.0.128:8765 .. note:: - Replace `192.168.0.128` with your actual server IP. - Ensure port `8765` is open and reachable. * Step-3 : Send multiple test messages from the client .. code-block:: shell hello how are you? message from client * Step-4 : Observe echo responses for each message .. code-block:: shell hello <-- (User typed) hello <-- (Echoed from server) how are you? <-- (User typed) how are you? <-- (Echoed from server) message from client <-- (User typed) message from client <-- (Echoed from server) .. note:: - Each message you type should be echoed immediately by the server. - The connection remains open during the exchange. * Step-5 : View server logs to confirm all messages are received .. code-block:: shell server:~$ tail -f /tmp/websocketd_log.txt Wed Aug 6 12:18:42 UTC 2025: Received: hello Wed Aug 6 12:18:45 UTC 2025: Received: how are you? Wed Aug 6 12:18:48 UTC 2025: Received: message from client .. note:: - Logs should include all the messages with timestamps. * Step-6 : Wireshark capture :download:`Download Wireshark capture ` **TESTCASE-3: Echo of Special Characters** * To verify that the WebSocket server correctly echoes back messages containing only special characters. .. note:: - This test ensures the server can process symbols and non-alphanumeric input without encoding issues. - The server should return the exact special character string. - Useful for validating shell-safe transmission and echo behavior. * Step-1 : Ensure the server is running .. code-block:: shell server:~$ websocketd --port=8765 --binary ./echo_and_log.sh * Step-2 : Connect to the server using `websocat` from the client .. code-block:: shell client:~$ websocat ws://192.168.0.128:8765 * Step-3 : Send special character message from the client .. code-block:: shell @@@#$$#$$%^^&%$^&&*^&*&&* * Step-4 : Observe echo response on the client .. code-block:: shell @@@#$$#$$%^^&%$^&&*^&*&&* <-- (Echoed from server) .. note:: - The echoed message should exactly match the input. - If any characters are missing or altered, the test fails. * Step-5 : View server logs to confirm the special character input .. code-block:: shell server:~$ tail -f /tmp/websocketd_log.txt Wed Aug 6 12:28:42 UTC 2025: Received: @@@#$$#$$%^^&%$^&&*^&*&&* .. note:: - The log should show the full special character message: * Step-6 : Wireshark capture :download:`Download Wireshark capture ` **TESTCASE-4: Client Forceful Termination Over IPv4** * To verify the WebSocket server correctly detects and logs abrupt client disconnection (without a proper WebSocket close handshake). .. note:: - This test simulates a forced shutdown (e.g., CTRL+C) of the WebSocket client. - The server should log the disconnection and remain stable. - The server must not crash or hang on abrupt termination. * Step-1 : Ensure the WebSocket server is running .. code-block:: shell server:~$ websocketd --port=8765 --binary ./echo_and_log.sh * Sample output: .. code-block:: shell Wed, 06 Aug 2025 10:04:13 +0000 | INFO | server | | Serving using application : ./echo_and_log.sh Wed, 06 Aug 2025 10:04:13 +0000 | INFO | server | | Starting WebSocket server : ws://user:8765/ * Step-2 : Connect to the server using `websocat` from the client .. code-block:: shell client:~$ websocat ws://192.168.0.128:8765 .. note:: - Replace `192.168.0.128` with your actual server IP. - Ensure port `8765` is open and reachable via IPv4. * Step-3 : Send a test message from the client .. code-block:: shell Hello from websocket * Step-4 : Observe echo response from the server .. code-block:: shell Hello from websocket <-- (User typed) Hello from websocket <-- (Echoed by server) * Step-5 : Forcefully terminate the client connection .. code-block:: shell Press CTRL+C in the terminal running websocat .. note:: - This action abruptly ends the WebSocket session without a proper close frame. * Step-6 : Verify server logs capture the disconnection .. code-block:: shell server:~$ cat /tmp/websocketd_log.txt * Expected output: .. code-block:: shell Wed Aug 6 10:04:27 UTC 2025 - Received: Hello from websocket * Step-7 : Check websocketd access logs for disconnect event * Sample server terminal output: .. code-block:: shell Wed, 06 Aug 2025 10:04:18 +0000 | ACCESS | session | url:'http://192.168.0.128:8765/' id:'1754474658605292037' remote:'192.168.0.129' command:'./echo_and_log.sh' origin:'file:' | CONNECT Wed, 06 Aug 2025 10:04:28 +0000 | ACCESS | session | url:'http://192.168.0.128:8765/' id:'1754474658605292037' remote:'192.168.0.129' command:'./echo_and_log.sh' origin:'file:' pid:'36128' | DISCONNECT * Step-8 : Confirm server is still running and accepting connections .. code-block:: shell client:~$ websocat ws://192.168.0.128:8765 .. note:: - If the connection is successful, the server is stable after forced termination. * Step-9 : Wireshark capture :download:`Download Wireshark capture ` **Expected Result** - The client disconnects abruptly. - The server logs the last message and a disconnection entry. - Server remains stable and responsive to new connections. **TESTCASE-5: Long Message** * To verify that the WebSocket server correctly echoes and logs a long message (500+ characters) sent by the client. .. note:: - The full long message should be echoed back intact. - The server log should contain the complete message with timestamps. * Step-1 : Ensure the server is running .. code-block:: shell server:~$ websocketd --port=8765 --binary ./echo_and_log.sh * Step-2 : Connect to the server using `websocat` from the client .. code-block:: shell client:~$ websocat ws://192.168.0.128:8765 .. note:: - Replace `192.168.0.128` with your actual server IP. - Ensure port `8765` is open and reachable. * Step-3 : Send a long message from the client .. code-block:: shell One morning, little Mia was getting ready for school when she noticed a button missing from her favorite sweater. She frowned, worried—this sweater was lucky! Instead of changing clothes, she grabbed her tiny sewing kit and, with careful fingers, stitched on a mismatched green button where the red one had been. At school, her friend Leo noticed it. “Cool button,” he said. “Thanks,” Mia smiled. “It’s not the same, but it works.” That day, she aced her spelling test, found a shiny penny on the ground, and was chosen to lead the class line. On the way home, Mia looked at the green button and thought, “Maybe this one’s lucky too.” * Step-4 : Observe the full message echoed back from the server * Step-5 : View server logs to confirm the full message is received .. code-block:: shell server:~$ cat /tmp/websocketd_log.txt Wed Aug 6 05:18:56 UTC 2025 - Received: One morning, little Mia was getting ready for school when she noticed a button missing from her favorite sweater. She frowned, worried—this sweater was lucky! Wed Aug 6 05:18:56 UTC 2025 - Received: Instead of changing clothes, she grabbed her tiny sewing kit and, with careful fingers, stitched on a mismatched green button where the red one had been. Wed Aug 6 05:18:56 UTC 2025 - Received: At school, her friend Leo noticed it. Wed Aug 6 05:18:56 UTC 2025 - Received: “Cool button,” he said. Wed Aug 6 05:18:56 UTC 2025 - Received: “Thanks,” Mia smiled. “It’s not the same, but it works.” Wed Aug 6 05:18:56 UTC 2025 - Received: That day, she aced her spelling test, found a shiny penny on the ground, and was chosen to lead the class line. Wed Aug 6 05:18:56 UTC 2025 - Received: On the way home, Mia looked at the green button and thought, “Maybe this one’s lucky too.” * Step-6 : Wireshark capture :download:`Download Wireshark capture ` **TESTCASE-6: Server-Initiated (Push) Communication** * To verify that the WebSocket server can push data to the client without the client initiating any messages. .. note:: - This test validates the full-duplex nature of WebSockets. - Client should receive messages passively (e.g., periodic timestamps). - No message is required from the client side. * Step-1 : Create and configure `push_server.sh` on the server .. code-block:: bash server:~$ nano push_server.sh #!/bin/bash # This script pushes time messages every 5 seconds while true; do echo "⏰ Server time: $(date)" sleep 5 done .. code-block:: bash server:~$ chmod +x push_server.sh * Step-2 : Start the server .. code-block:: bash server:~$ websocketd --port=8765 ./push_server.sh * Step-3 : Connect to the server from the client .. code-block:: bash client:~$ websocat ws://192.168.0.128:8765 .. note:: - Replace `192.168.0.128` with the actual server IP address. - Ensure port `8765` is open and not blocked by a firewall. - Do **not** send any input after connecting. * Step-4 : Observe periodic messages from the server * Example output on client: .. code-block:: text ⏰ Server time: Wed Aug 6 07:13:42 UTC 2025 ⏰ Server time: Wed Aug 6 07:13:47 UTC 2025 ⏰ Server time: Wed Aug 6 07:13:52 UTC 2025 ⏰ Server time: Wed Aug 6 07:13:57 UTC 2025 * Step-5 : Check for errors or disconnection in server logs .. code-block:: text Wed, 06 Aug 2025 07:13:38 +0000 | INFO | server | | Serving using application : ./push_server.sh Wed, 06 Aug 2025 07:13:38 +0000 | INFO | server | | Starting WebSocket server : ws://pavithra:8765/ Wed, 06 Aug 2025 07:13:42 +0000 | ACCESS | session | url:'http://192.168.0.128:8765/' id:'1754464422705762813' remote:'192.168.0.129' command:'./push_server.sh' origin:'file:' | CONNECT Wed, 06 Aug 2025 07:14:19 +0000 | ERROR | process | Unexpected error while reading STDOUT: file already closed .. note:: - You may see errors after client disconnects. - This is expected if the server process tries to write to a closed socket. * Step-6 : Wireshark capture :download:`Download Wireshark capture ` .. _WebSocket_step20: .. tab-set:: .. tab-item:: WebSocket Basic Setup on Ubuntu using IPv6 **Installation & Setup Steps** * Step 1: Install websocketd on the server .. code-block:: shell server:~$ sudo apt update server:~$ sudo apt install websocketd * Step 2: Create the echo_and_log.sh script on the server (if not already) .. code-block:: shell server:~$ nano echo_and_log.sh * Paste the following content: .. code-block:: shell #!/bin/bash while true; do read input if [ -n "$input" ]; then echo "$(date) - Received: $input" >> /tmp/websocketd_log.txt echo "$input" fi done * Save and exit. * Step 3: Make the script executable .. code-block:: shell server:~$ chmod +x echo_and_log.sh * Step 4: Open firewall port 8765 for IPv6 traffic (if firewall enabled) * Check firewall status: .. code-block:: shell server:~$ sudo ufw status * If enabled, allow port 8765 for both IPv4 & IPv6: .. code-block:: shell server:~$ sudo ufw allow 8765 server:~$ sudo ufw enable # if not already enabled * Step 5: Start the WebSocket server listening on IPv6 .. code-block:: shell server:~$ websocketd --port=8765 --address=[::] --binary ./echo_and_log.sh * You should see logs like: .. code-block:: shell INFO | server | | Serving using application : ./echo_and_log.sh INFO | server | | Starting WebSocket server : ws://[::]:8765/ * Step 6: Install websocat on the client machine (if not already) .. code-block:: shell client:~$ sudo apt install curl client:~$ curl -LO https://github.com/vi/websocat/releases/download/v1.7.0/websocat_amd64-linux client:~$ chmod +x websocat_amd64-linux client:~$ sudo mv websocat_amd64-linux /usr/local/bin/websocat * Step 7: Connect client to server via websocat over IPv6 * Get your server IPv6 address: .. code-block:: shell client:~$ ip -6 addr show * Connect using: .. code-block:: shell client:~$ websocat ws://[2001:db8::128]:8765 .. note :: * Replace ``2001:db8::128`` with your server’s actual IPv6 address. * If using a link-local address (fe80::...), append the interface name, e.g.: ``ws://[fe80::1234:abcd%eth0]:8765`` **TESTCASE-1: Successful Echo Message over IPv6** * To test WebSocket communication between a Linux server using `websocketd` and a client using `websocat` over IPv6. .. note:: - Ensure both client and server are reachable over IPv6. - Make sure port `8765` is open and not blocked by firewall for IPv6. - Use the correct server IPv6 address (e.g., `2001:db8::128`). * Step-1 : Start WebSocket server listening on IPv6 address .. code-block:: shell server:~$ websocketd --port=8765 --address=[::] --binary ./echo_and_log.sh Wed, 06 Aug 2025 09:23:42 +0000 | INFO | server | | Serving using application : ./echo_and_log.sh Wed, 06 Aug 2025 09:23:42 +0000 | INFO | server | | Starting WebSocket server : ws://[::]user:8765/ * Step-2 : Connect to the server using `websocat` with IPv6 address .. code-block:: shell client:~$ websocat ws://[2001:db8::128]:8765 .. note:: - Replace `2001:db8::128` with your actual server IPv6 address. - Ensure the client machine can reach this IPv6 address and port (8765). * Step-3 : Send test message from client .. code-block:: shell HELLO .. note:: - This message will be echoed back to the client. - It will also be logged in `/tmp/websocketd_log.txt` on the server. * Step-4 : Send test message from client and observe echo .. code-block:: shell HELLO <-- (User typed message) HELLO <-- (Server echo response) .. note:: - The first line is the message you typed. - The second line is the echo response from the WebSocket server. * Step-5 : View server log to confirm received messages .. code-block:: shell server:~$ cat /tmp/websocketd_log.txt Wed Aug 6 09:23:51 UTC 2025 - Received: HELLO .. note:: - This confirms the message was received and logged by the server. * Step-6 : Wireshark capture :download:`Download Wireshark capture ` **TESTCASE-2: Multiple Message Echo in Single Session over IPv6** * To validate WebSocket echo functionality when sending multiple messages from a client in one continuous connection over IPv6. .. note:: - This test ensures that the WebSocket server maintains persistent session state. - All messages sent should be echoed back individually. - The server log should record each message with a timestamp. * Step-1 : Ensure the server is running listening on IPv6 .. code-block:: shell server:~$ websocketd --port=8765 --address=[::] --binary ./echo_and_log.sh * Step-2 : Connect to the server using `websocat` from the client over IPv6 .. code-block:: shell client:~$ websocat ws://[2001:db8::128]:8765 .. note:: - Replace `2001:db8::128` with your actual server IPv6 address. - Ensure port `8765` is open and reachable on IPv6. * Step-3 : Send multiple test messages from the client .. code-block:: shell hello how are you? message from client * Step-4 : Observe echo responses for each message .. code-block:: shell hello <-- (User typed) hello <-- (Echoed from server) how are you? <-- (User typed) how are you? <-- (Echoed from server) message from client <-- (User typed) message from client <-- (Echoed from server) .. note:: - Each message you type should be echoed immediately by the server. - The connection remains open during the exchange. * Step-5 : View server logs to confirm all messages are received .. code-block:: shell server:~$ tail -f /tmp/websocketd_log.txt Wed Aug 6 12:18:42 UTC 2025: Received: hello Wed Aug 6 12:18:45 UTC 2025: Received: how are you? Wed Aug 6 12:18:48 UTC 2025: Received: message from client .. note:: - Logs should include all the messages with timestamps. * Step-6 : (Optional) Wireshark capture :download:`Download Wireshark capture ` **TESTCASE-3: Echo of Special Characters over IPv6** * To verify that the WebSocket server correctly echoes back messages containing only special characters over IPv6. .. note:: - This test ensures the server can process symbols and non-alphanumeric input without encoding issues. - The server should return the exact special character string. - Useful for validating shell-safe transmission and echo behavior. * Step-1 : Ensure the server is running with IPv6 address .. code-block:: shell server:~$ websocketd --port=8765 --address=[::] --binary ./echo_and_log.sh * Step-2 : Connect to the server using `websocat` from the client over IPv6 .. code-block:: shell client:~$ websocat ws://[2001:db8::128]:8765 * Step-3 : Send special character message from the client .. code-block:: shell #@#$%^&*()_+%%^@@!#@#$%$^%&^&**&!~~@#@$%^%&^&*(_))!@!@$#%$^%&^ * Step-4 : Observe echo response on the client .. code-block:: shell #@#$%^&*()_+%%^@@!#@#$%$^%&^&**&!~~@#@$%^%&^&*(_))!@!@$#%$^%&^ <-- (Echoed from server) .. note:: - The echoed message should exactly match the input. - If any characters are missing or altered, the test fails. * Step-5 : View server logs to confirm the special character input .. code-block:: shell server:~$ tail -f /tmp/websocketd_log.txt Wed Aug 6 09:39:16 UTC 2025 - Received: #@#$%^&*()_+%%^@@!#@#$%$^%&^&**&!~~@#@$%^%&^&*(_))!@!@$#%$^%&^ .. note:: - The log should show the full special character message: * Step-6 : Wireshark capture :download:`Download Wireshark capture ` **TESTCASE-4: Client Forceful Termination Over IPv6** * To verify the WebSocket server correctly detects and logs abrupt client disconnection (without a proper WebSocket close handshake) over IPv6. .. note:: - This test simulates a forced shutdown (e.g., CTRL+C) of the WebSocket client. - The server should log the disconnection and remain stable. - The server must not crash or hang on abrupt termination. * Step-1 : Ensure the WebSocket server is running with IPv6 address .. code-block:: shell server:~$ websocketd --port=8765 --address=[::] --binary ./echo_and_log.sh * Sample output: .. code-block:: shell Wed, 06 Aug 2025 09:57:01 +0000 | INFO | server | | Serving using application : ./echo_and_log.sh Wed, 06 Aug 2025 09:57:01 +0000 | INFO | server | | Starting WebSocket server : ws://pavithra:8765/ * Step-2 : Connect to the server using `websocat` from the client over IPv6 .. code-block:: shell client:~$ websocat ws://[2001:db8::128]:8765 .. note:: - Replace `[2001:db8::128]` with your actual server IPv6 address. - Ensure port `8765` is open and reachable via IPv6. * Step-3 : Send a test message from the client .. code-block:: shell hello before termination * Step-4 : Observe echo response from the server .. code-block:: shell hello before termination <-- (User typed) hello before termination <-- (Echoed by server) * Step-5 : Forcefully terminate the client connection .. code-block:: shell Press CTRL+C in the terminal running websocat .. note:: - This action abruptly ends the WebSocket session without a proper close frame. * Step-6 : Verify server logs capture the disconnection .. code-block:: shell server:~$ cat /tmp/websocketd_log.txt * Expected output: .. code-block:: shell Wed Aug 6 09:57:15 UTC 2025 - Received: hello before termination * Step-7 : Check websocketd access logs for disconnect event * Sample server terminal output: .. code-block:: shell Wed, 06 Aug 2025 09:57:05 +0000 | ACCESS | session | url:'http://[2001:db8::128]:8765/' id:'1754474225741017963' remote:'2001:db8::129' command:'./echo_and_log.sh' origin:'file:' | CONNECT Wed, 06 Aug 2025 09:57:19 +0000 | ACCESS | session | url:'http://[2001:db8::128]:8765/' id:'1754474225741017963' remote:'2001:db8::129' command:'./echo_and_log.sh' origin:'file:' pid:'36006' | DISCONNECT * Step-8 : Confirm server is still running and accepting connections .. code-block:: shell client:~$ websocat ws://[2001:db8::128]:8765 .. note:: - If the connection is successful, the server is stable after forced termination. * Step-9 : Wireshark capture :download:`Download Wireshark capture ` **Expected Result** - The client disconnects abruptly. - The server logs the last message and a disconnection entry. - Server remains stable and responsive to new connections. **TESTCASE-5: Long Message over IPv6** * To verify that the WebSocket server correctly echoes and logs a long message (500+ characters) sent by the client over an IPv6 connection. . note:: - The full long message should be echoed back intact. - The server log should contain the complete message with timestamps. * Step-1 : Ensure the server is running with IPv6 support .. code-block:: shell server:~$ websocketd --port=8765 --address=[::] --binary ./echo_and_log.sh .. note:: - `--address=[::]` ensures the server listens on all IPv6 interfaces. - Confirm the server log shows IPv6-based connection entries, e.g.: * Step-2 : Connect to the server using `websocat` from the client over IPv6 .. code-block:: shell client:~$ websocat ws://[2001:db8::128]:8765 .. note:: - Replace `[2001:db8::128]` with your actual server IPv6 address. - Ensure the IPv6 address is reachable and port `8765` is open. * Step-3 : Send a long message from the client .. code-block:: shell One morning, little Mia was getting ready for school when she noticed a button missing from her favorite sweater. She frowned, worried—this sweater was lucky! Instead of changing clothes, she grabbed her tiny sewing kit and, with careful fingers, stitched on a mismatched green button where the red one had been. At school, her friend Leo noticed it. “Cool button,” he said. “Thanks,” Mia smiled. “It’s not the same, but it works.” That day, she aced her spelling test, found a shiny penny on the ground, and was chosen to lead the class line. On the way home, Mia looked at the green button and thought, “Maybe this one’s lucky too.” * Step-4 : Observe the full message echoed back from the server * Step-5 : View server logs to confirm the full message is received .. code-block:: shell server:~$ cat /tmp/websocketd_log.txt Wed Aug 6 09:42:32 UTC 2025 - Received: One morning, little Mia was getting ready for school when she noticed a button missing from her favorite sweater. She frowned, worried—this sweater was lucky! Wed Aug 6 09:42:32 UTC 2025 - Received: Instead of changing clothes, she grabbed her tiny sewing kit and, with careful fingers, stitched on a mismatched green button where the red one had been. Wed Aug 6 09:42:32 UTC 2025 - Received: At school, her friend Leo noticed it. Wed Aug 6 09:42:32 UTC 2025 - Received: “Cool button,” he said. Wed Aug 6 09:42:32 UTC 2025 - Received: “Thanks,” Mia smiled. “It’s not the same, but it works.” Wed Aug 6 09:42:32 UTC 2025 - Received: That day, she aced her spelling test, found a shiny penny on the ground, and was chosen to lead the class line. Wed Aug 6 09:42:32 UTC 2025 - Received: On the way home, Mia looked at the green button and thought, “Maybe this one’s lucky too.” * Step-6 : Wireshark capture :download:`Download Wireshark capture ` **TESTCASE-6 – Server-Initiated (Push) Communication over IPv6** * To verify that the WebSocket server can push data to the client without the client initiating any messages, using IPv6. .. note:: - This test validates the full-duplex nature of WebSockets. - Client should receive messages passively (e.g., periodic timestamps). - No message is required from the client side. * Step-1 : Create and configure `push_server.sh` on the server .. code-block:: bash server:~$ nano push_server.sh #!/bin/bash # This script pushes time messages every 5 seconds while true; do echo "⏰ Server time: $(date)" sleep 5 done .. code-block:: bash server:~$ chmod +x push_server.sh * Step-2 : Start the server with IPv6 support .. code-block:: bash server:~$ websocketd --port=8765 --address=[::] ./push_server.sh .. note:: - `--address=[::]` ensures the server listens on all IPv6 interfaces. - Confirm the log shows an IPv6 connection: * Step-3 : Connect to the server from the client using IPv6 .. code-block:: bash client:~$ websocat ws://[2001:db8::128]:8765 .. note:: - Replace `[2001:db8::128]` with the actual server IPv6 address. - Ensure port `8765` is open and reachable via IPv6. - Do **not** send any input after connecting. * Step-4 : Observe periodic messages from the server Example output on client: .. code-block:: text ⏰ Server time: Wed Aug 6 09:45:29 UTC 2025 ⏰ Server time: Wed Aug 6 09:45:34 UTC 2025 ⏰ Server time: Wed Aug 6 09:45:39 UTC 2025 * Step-5 : Check for errors or disconnection in server logs .. code-block:: text Wed, 06 Aug 2025 09:45:24 +0000 | INFO | server | | Serving using application : ./push_server.sh Wed, 06 Aug 2025 09:45:24 +0000 | INFO | server | | Starting WebSocket server : ws://pavithra:8765/ Wed, 06 Aug 2025 09:45:29 +0000 | ACCESS | session | url:'http://[2001:db8::128]:8765/' id:'1754473529758530712' remote:'2001:db8::129' command:'./push_server.sh' origin:'file:' | CONNECT .. note:: - You may see errors after the client disconnects. - This is expected if the server process tries to write to a closed socket. * Step-6 : Wireshark capture :download:`Download Wireshark capture ` .. _WebSocket_step6: .. tab-set:: .. tab-item:: WebSocket Protocol Packet Details **Client Handshake Request Packet** .. csv-table:: :file: ./WebSocket/WebSocket_packetdetails1.csv :widths: 10,20,30,10 :header-rows: 1 **Server Handshake Response Packet** .. csv-table:: :file: ./WebSocket/WebSocket_packetdetails2.csv :widths: 10,20,30,10 :header-rows: 1 **Data Frame Packet** .. csv-table:: :file: ./WebSocket/WebSocket_packetdetails3.csv :widths: 10,20,30,10 :header-rows: 1 **Control Frame Packet** .. csv-table:: :file: ./WebSocket/WebSocket_packetdetails4.csv :widths: 10,20,30,10 :header-rows: 1 .. _WebSocket_step7: .. tab-set:: .. tab-item:: WebSocket Usecases .. csv-table:: :file: ./WebSocket/WebSocket_usecases.csv :widths: 10,20,30 :header-rows: 1 .. _WebSocket_step8: .. tab-set:: .. tab-item:: WebSocket Basic Features .. csv-table:: :file: ./WebSocket/WebSocket_basicfeatures.csv :widths: 10,10,30 :header-rows: 1 .. _WebSocket_step9: .. tab-set:: .. tab-item:: WebSocket Feature : Full Duplex Communication **Full Duplex Communication - Testcases** .. csv-table:: :file: ./WebSocket/WebSocket_feature1_Full_Duplex_Communication_testcases.csv :widths: 10,10,30,20 :header-rows: 1 .. _WebSocket_step10: .. tab-set:: .. tab-item:: WebSocket Feature : Persistent Connection **Persistent Connection - Testcases** .. csv-table:: :file: ./WebSocket/WebSocket_feature2_Persistent_Connection_testcases.csv :widths: 10,10,30,20 :header-rows: 1 .. _WebSocket_step11: .. tab-set:: .. tab-item:: WebSocket Feature : Low Latency **Low Latency - Testcases** .. csv-table:: :file: ./WebSocket/WebSocket_feature3_Low_Latency_testcases.csv :widths: 10,10,30,20 :header-rows: 1 .. _WebSocket_step12: .. tab-set:: .. tab-item:: WebSocket Feature : Light weight Protocol **Light weight Protocol - Testcases** .. csv-table:: :file: ./WebSocket/WebSocket_feature4_Light_weight_Protocol_testcases.csv :widths: 10,10,30,20 :header-rows: 1 .. _WebSocket_step13: .. tab-set:: .. tab-item:: WebSocket Feature : Real Time Data Transfer **Real Time Data Transfer - Testcases** .. csv-table:: :file: ./WebSocket/WebSocket_feature5_Real_Time_Data_Transfer_testcases.csv :widths: 10,10,30,20 :header-rows: 1 .. _WebSocket_step14: .. tab-set:: .. tab-item:: WebSocket Feature : Cross Platform Support **Cross Platform Support - Testcases** .. csv-table:: :file: ./WebSocket/WebSocket_feature6_Cross_Platform_Support_testcases.csv :widths: 10,10,30,20 :header-rows: 1 .. _WebSocket_step15: .. tab-set:: .. tab-item:: WebSocket Feature : Firewall Friendly **Firewall Friendly - Testcases** .. csv-table:: :file: ./WebSocket/WebSocket_feature7_Firewall_Friendly_testcases.csv :widths: 10,10,30,20 :header-rows: 1 .. _WebSocket_step16: .. tab-set:: .. tab-item:: WebSocket Feature : Binary and Text Support **Binary and Text Support - Testcases** .. csv-table:: :file: ./WebSocket/WebSocket_feature8_Binary_and_Text_Support_testcases.csv :widths: 10,10,30,20 :header-rows: 1 .. _WebSocket_step17: .. tab-set:: .. tab-item:: WebSocket Feature : Built in Ping Pong Frames **Built in Ping Pong Frames - Testcases** .. csv-table:: :file: ./WebSocket/WebSocket_feature9_Built_in_Ping_Pong_Frames_testcases.csv :widths: 10,10,30,20 :header-rows: 1 .. _WebSocket_step21: .. tab-set:: .. tab-item:: WebSocket Feature : Protocol Upgrade from HTTP **Protocol Upgrade from HTTP - Testcases** .. csv-table:: :file: ./WebSocket/WebSocket_feature10_Protocol_Upgrade_from_HTTP_testcases.csv :widths: 10,10,30,20 :header-rows: 1 .. _WebSocket_step18: .. tab-set:: .. tab-item:: Reference links * Reference links