# WebSocket close code 1002: Protocol Error

- **ID:** `api/websocket-close-code-1002-protocol-error`
- **Domain:** api
- **Category:** protocol_error
- **Error Code:** `1002`
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

WebSocket frame was malformed or contained invalid data, such as a reserved opcode or fragmented control frame.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| RFC 6455 | active | — | — |
| ws library 8.x | active | — | — |
| Socket.IO 4.x | active | — | — |
| Spring WebSocket 6.0 | active | — | — |
| Node.js ws 8.14+ | active | — | — |

## Workarounds

1. **Validate that control frames (e.g., ping, pong, close) are not fragmented. In JavaScript, ensure you are not sending a fragmented ping frame:
// Bad: Sending a fragmented ping
ws.send(Buffer.from('ping'), { fin: false, opcode: 0x9 }); // Wrong!
// Good: Send a complete ping frame
ws.ping(); // Uses correct non-fragmented frame** (90% success)
   ```
   Validate that control frames (e.g., ping, pong, close) are not fragmented. In JavaScript, ensure you are not sending a fragmented ping frame:
// Bad: Sending a fragmented ping
ws.send(Buffer.from('ping'), { fin: false, opcode: 0x9 }); // Wrong!
// Good: Send a complete ping frame
ws.ping(); // Uses correct non-fragmented frame
   ```
2. **Check for reserved opcodes (0x3-0x7, 0xB-0xF) in the frame. If using a custom WebSocket library, ensure opcode is in {0x0, 0x1, 0x2, 0x8, 0x9, 0xA}.** (85% success)
   ```
   Check for reserved opcodes (0x3-0x7, 0xB-0xF) in the frame. If using a custom WebSocket library, ensure opcode is in {0x0, 0x1, 0x2, 0x8, 0x9, 0xA}.
   ```

## Dead Ends

- **** — The error is immediate and not related to timeouts. (90% fail)
- **** — The root cause is in the client's frame construction, not server state. (80% fail)
