WebSocket close codes
Type the code from your close event. You get what it means, which document defines it, and what usually causes it.
Everything runs in your browser
Spec: RFC 6455 § 7.4IANA WebSocket Close Code Number Registry
Any number. Close codes run from 1000 to 4999.
All WebSocket close codes (status codes) from 1000 to 1015
The close code is also called the status code of a WebSocket connection. Here is every code from 1000 to 1015 with its registry name, its meaning and the usual causes.
| Code | Name | What it means | On the wire |
|---|---|---|---|
| 1000 | Normal Closure | The connection closed as intended. Both sides were done.
| yes |
| 1001 | Going Away | One side is shutting down: the page is unloading, or the server is stopping.
| yes |
| 1002 | Protocol Error | A frame broke the WebSocket protocol. The stream can no longer be read reliably, so the connection closes.
| yes |
| 1003 | Unsupported Data | The side that closed understands the data type but does not accept it. A text-only server got a binary message, for example.
| yes |
| 1004 | Reserved | Reserved, with no meaning assigned. Old drafts used it for «frame too large», and 1009 does that job now.
| no |
| 1005 | No Status Received | A Close frame arrived, but it carried no status code. The API reports 1005 instead of leaving the field empty.
| no |
| 1006 | Abnormal Closure | No Close frame arrived and the connection just went away. The code says nothing about the cause.
| no |
| 1007 | Invalid Frame Payload Data | The payload does not match the frame type. Most often a text frame that is not valid UTF‑8.
| yes |
| 1008 | Policy Violation | The message broke an application rule and no more exact code fits. It is the generic «you cannot do that».
| yes |
| 1009 | Message Too Big | The message is bigger than the other side will process.
| yes |
| 1010 | Mandatory Extension | The client asked for an extension, the server did not negotiate it, so the client closes. Only clients send this code, because a server fails the handshake instead.
| yes |
| 1011 | Internal Error | The server hit an unexpected condition and cannot finish the request. It is the WebSocket version of a 500.
| yes |
| 1012 | Service Restart | The server is restarting. Reconnect, but wait a bit first.
| yes |
| 1013 | Try Again Later | The server cannot take the connection right now. It is overloaded or throttling.
| yes |
| 1014 | Bad Gateway | A gateway or a proxy got an invalid response from the server behind it. It is the WebSocket version of a 502.
| yes |
| 1015 | TLS Handshake | The TLS handshake failed. Browsers never use this code: they report 1006 instead.
| no |
The ranges
- 1000–2999
- Belongs to the protocol and the IANA registry. Nothing of yours goes here, and a browser refuses to send any of it except 1000.
- 3000–3999
- Libraries, frameworks and apps, registered with IANA. Look the meaning up in the docs of whatever sent the code.
- 4000–4999
- Private use. You define the meaning and register nothing.
Questions about WebSocket close codes
What is a WebSocket close code?
A number from 1000 to 4999 that says why the connection ended. The side that closes puts it in the first two bytes of the Close frame and may add a reason, up to 123 bytes of text. In a browser it arrives in the close event as event.code, next to event.reason and event.wasClean. Libraries and documentation also call it the status code or the error code.
What does WebSocket close code 1000 mean?
A normal closure. One side finished its work, sent a Close frame with 1000, and the other side closed back. It is not an error, there is nothing to fix, and the client should not reconnect.
Why does a WebSocket disconnect by itself?
Most often something between the client and the server drops a connection that has been silent for too long. nginx does it after 60 seconds by default (proxy_read_timeout), and load balancers have a limit of their own. The browser then reports 1006, and the drops come at the same interval. Send a ping or a small message more often than the shortest timeout on the path. Any other code names the cause directly: 1001 is a server stopping or a page unloading, 1011 is an error on the server.
What is the difference between 1005 and 1006?
With 1005 a Close frame arrived but carried no code, so the closing handshake finished and wasClean is true. With 1006 no Close frame arrived at all: the connection was cut, and wasClean is false. Neither code is ever sent. The API fills both in on your side.
Which close codes can I send from my own code?
In a browser close() takes 1000 or a number from 3000 to 4999. Any other code throws InvalidAccessError, and a reason longer than 123 bytes throws SyntaxError. For codes of your own use 4000–4999. A server library lets you send more of the list, but 1004, 1005, 1006 and 1015 must never go into a Close frame.
After which close codes should a client reconnect?
Reconnect after 1001, 1006, 1012, 1013 and 1014, and make the delay longer with every attempt. After 1011 a few attempts are enough. Do not reconnect after 1000: the connection closed on purpose. After 1002, 1003, 1007, 1008, 1009 and 1010 the same client fails the same way, so fix the cause first.
Is 101 a WebSocket close code?
No. 101 Switching Protocols is an HTTP status code. The server sends it once, in answer to the Upgrade request, and it means the connection opened. Close codes appear at the end of the connection. When the server answers the handshake with anything other than 101, the browser reports 1006 and does not show the HTTP status to the script.
See also
Want to see more?
View in Solpuga