WebSocket frames and message types
frame structure, opcode types, FIN bit, masking, text vs binary frames, ping/pong frames, close frame, fragmentation
What a WebSocket Frame Looks Like
Every message over a WebSocket connection is wrapped in a frame. The frame header encodes:
FIN bit: 1 if this is the final fragment of a message.
Opcode: What kind of frame this is.
MASK bit + masking key: Client-to-server frames must be masked. Server-to-client frames are not.
Payload length: 7 bits, extended to 16 or 64 bits for larger payloads.
Opcode Reference
The four opcodes you will encounter in practice:
0x1— Text frame (UTF-8 string)0x2— Binary frame (ArrayBuffer / Buffer)0x8— Close frame (starts the closing handshake)0x9/0xA— Ping / Pong (keep-alive)
You rarely construct frames manually. Libraries handle framing. But knowing opcodes helps you read wire captures in Chrome DevTools → Network → WS tab, where you can see every frame sent and received, its type, and its payload. Binary frames show as a byte count; text frames show the raw string.
Fragmentation lets large messages split across multiple frames with FIN=0 on all but the last. Most libraries reassemble fragments before surfacing the message to your callback.
