Script Valley
WebSockets & Real-Time Applications
How WebSockets Actually Work/Assessment

Practice & Assessment

Test your understanding of How WebSockets Actually Work

Multiple Choice Questions

6
1

What HTTP status code does a server return to successfully upgrade a connection to WebSocket?

2

Which WebSocket readyState value indicates the connection is open and messages can be sent?

3

What does a WebSocket close code of 1006 indicate?

4

Why must client-to-server WebSocket frames be masked?

5

Which WebSocket opcode represents a ping frame?

6

What is the purpose of the Sec-WebSocket-Key header during the handshake?

Coding Challenges

1
1

Parse a Raw WebSocket Handshake Response

Given a string containing a raw HTTP 101 response with WebSocket headers, write a function parseHandshake(rawResponse) that returns an object with keys: statusCode (number), secWebSocketAccept (string), and isValid (boolean, true only if status is 101 and Sec-WebSocket-Accept is present). Input: a multi-line string of raw HTTP headers. Output: a plain JS/Python object. Constraints: do not use any HTTP parsing library; use only string methods or regex. Estimated time: 20 minutes.

Easy

Mini Project

1

WebSocket Connection Inspector CLI

Build a Node.js CLI tool that connects to a given WebSocket URL (passed as a command-line argument), prints the full HTTP upgrade request headers, prints the 101 response headers, then enters an interactive mode where the user can type a message and press Enter to send it as a text frame. Each received frame should be printed with a timestamp, direction indicator (←), and the raw payload. On SIGINT (Ctrl+C) the tool should send a close frame with code 1000 and exit cleanly. Use only the built-in ws npm package and no other WebSocket abstractions.

Easy