How WebSocket Closing Works
Either side can initiate a close handshake by sending a close frame with a status code and optional reason. The other side responds with its own close frame.
Close Codes
Normal Codes (1000-1003)
- 1000 Normal Closure — Clean shutdown, no errors.
- 1001 Going Away — Server shutting down or client navigating away.
- 1002 Protocol Error — Malformed frame or invalid data.
- 1003 Unsupported Data — Received data type the endpoint can't process.
Error Codes (1008-1011)
- 1008 Policy Violation — Message violates server policy.
- 1009 Message Too Big — Message exceeds size limit.
- 1010 Mandatory Extension — Client expected extension negotiation.
- 1011 Internal Error — Server hit an unexpected condition.
Special Codes
- 1005 No Status — No close code was provided (should not be sent in frames).
- 1006 Abnormal Closure — Connection dropped without a close frame. This is the most common "error" — network disconnection, server crash, etc.
- 1015 TLS Handshake — TLS failure (also should not be sent in frames).
Debugging 1006
1006 means the connection was lost without a proper close handshake. Common causes:
- Server process crashed
- Network interruption (WiFi switch, mobile data loss)
- Load balancer timeout (idle connection)
- Proxy/firewall dropped the connection
Solution: Implement reconnection logic with exponential backoff.