What is a WebSocket frame?

Answer

A WebSocket frame is the basic unit of data transmission in the WebSocket protocol. Unlike HTTP which sends full messages, WebSocket organizes data into frames that can be sent in a stream. A frame consists of: a FIN bit (indicates if this is the final fragment), an opcode (0x1 for text, 0x2 for binary, 0x8 for close, 0x9 for ping, 0xA for pong), a mask bit (client-to-server frames must be masked for security), a payload length (7-bit, 7+16-bit, or 7+64-bit encoding), an optional masking key (4 bytes for client frames), and the payload data. The frame header overhead is only 2–14 bytes, making WebSocket extremely efficient for high-frequency small messages compared to HTTP's kilobytes of headers per request.