# WebSocket close code 1008: Policy Violation

- **ID:** `api/websocket-close-code-1008-policy-violation`
- **Domain:** api
- **Category:** protocol_error
- **Error Code:** `1008`
- **Verification:** ai_generated
- **Fix Rate:** 75%

## Root Cause

Server closed WebSocket connection due to a policy violation such as invalid authentication, unsupported subprotocol, or message format violation.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| WebSocket RFC 6455 | active | — | — |
| Socket.IO 4.7.0 | active | — | — |
| ws@8.16.0 (Node.js) | active | — | — |
| Spring WebSocket 6.1.0 | active | — | — |

## Workarounds

1. **Check and refresh authentication token before reconnecting: `ws = new WebSocket('wss://api.example.com/ws', { headers: { Authorization: `Bearer ${newToken}` } })`** (90% success)
   ```
   Check and refresh authentication token before reconnecting: `ws = new WebSocket('wss://api.example.com/ws', { headers: { Authorization: `Bearer ${newToken}` } })`
   ```
2. **Verify the subprotocol matches server expectations: `ws = new WebSocket('wss://api.example.com/ws', ['graphql-ws'])`** (80% success)
   ```
   Verify the subprotocol matches server expectations: `ws = new WebSocket('wss://api.example.com/ws', ['graphql-ws'])`
   ```
3. **Ensure messages are in the expected format (e.g., JSON): `ws.send(JSON.stringify({ type: 'subscribe', channel: 'updates' }))`** (85% success)
   ```
   Ensure messages are in the expected format (e.g., JSON): `ws.send(JSON.stringify({ type: 'subscribe', channel: 'updates' }))`
   ```

## Dead Ends

- **** — Reconnecting without addressing the underlying policy violation (e.g., expired token) (85% fail)
- **** — Assuming it's a network timeout and increasing timeout values (60% fail)
- **** — Changing WebSocket URL path without verifying authentication credentials (70% fail)
