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.

CodeNameWhat it meansOn the wire
1000Normal ClosureThe connection closed as intended. Both sides were done.
  • Your code called close(1000) and the other side closed back.
  • The server finished its work and closed with 1000.
  • Careful: close() with no arguments sends no status code at all, and the event then usually reports 1005.
yes
1001Going AwayOne side is shutting down: the page is unloading, or the server is stopping.
  • A page reload, or the visitor navigating away.
  • A planned restart or a deploy on the server.
  • A load balancer took the instance out of the pool.
yes
1002Protocol ErrorA frame broke the WebSocket protocol. The stream can no longer be read reliably, so the connection closes.
  • The client sent an unmasked frame. RFC 6455 makes the client mask every frame it sends.
  • RSV bits set with no extension negotiated.
  • Broken fragmentation: a continuation frame with no start, or a text frame in the middle of one.
  • Something in between rewrote the frames, usually a proxy that does not understand Upgrade.
yes
1003Unsupported DataThe side that closed understands the data type but does not accept it. A text-only server got a binary message, for example.
  • The client sent an ArrayBuffer or a Blob where the server takes text.
  • The server sent a binary frame to a client that reads text only.
  • One end started sending binary after a deploy and the other end never did.
yes
1004ReservedReserved, with no meaning assigned. Old drafts used it for «frame too large», and 1009 does that job now.
  • If you see 1004, a library made it up.
  • A browser will not send it either: close(1004) throws.
no
1005No Status ReceivedA Close frame arrived, but it carried no status code. The API reports 1005 instead of leaving the field empty.
  • The other side called close() with no arguments, so the Close frame carried no code.
  • A library on the other end cannot set a code.
  • This is not a network failure. The closing handshake finished, so wasClean is true here and false for 1006.
no
1006Abnormal ClosureNo Close frame arrived and the connection just went away. The code says nothing about the cause.
  • A proxy, a load balancer or nginx dropped an idle connection on a timeout.
  • The server process crashed or was killed before it could send a Close frame.
  • The network changed or died: Wi-Fi to cellular, a tunnel, a laptop falling asleep.
  • Antivirus software, a VPN or a corporate proxy tore down a connection it did not understand.
  • The handshake never finished. The server answered something other than 101, and the browser still reports 1006.
no
1007Invalid Frame Payload DataThe payload does not match the frame type. Most often a text frame that is not valid UTF‑8.
  • Bytes built outside UTF‑8, such as a string sliced per character instead of per byte.
  • Binary data sent inside a text frame.
  • A multi-byte character cut in half at a fragment boundary.
yes
1008Policy ViolationThe message broke an application rule and no more exact code fits. It is the generic «you cannot do that».
  • Authentication failed, or the token expired.
  • A rate limit was hit.
  • The client asked for something it has no right to.
  • The server picks 1008 over 1003 or 1009 to hide what it objected to.
yes
1009Message Too BigThe message is bigger than the other side will process.
  • A message size limit in the server's library. Check its default, they differ a lot.
  • A whole file pushed into the socket instead of being sent in chunks.
  • A buffer limit in the proxy in front of the server.
yes
1010Mandatory ExtensionThe 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.
  • The client needs permessage-deflate and the server did not enable it.
  • The server agreed to the extension with parameters the client will not take.
yes
1011Internal ErrorThe server hit an unexpected condition and cannot finish the request. It is the WebSocket version of a 500.
  • An unhandled exception in the message handler.
  • A database or a queue the handler depends on went away.
  • The server closes because its state no longer adds up.
yes
1012Service RestartThe server is restarting. Reconnect, but wait a bit first.
  • A planned deploy or a process restart.
  • A configuration change that needs a restart.
yes
1013Try Again LaterThe server cannot take the connection right now. It is overloaded or throttling.
  • More connections than the server is prepared to serve.
  • A connection rate limit per address.
  • Deliberate shedding: the service drops some clients to carry the load.
yes
1014Bad GatewayA gateway or a proxy got an invalid response from the server behind it. It is the WebSocket version of a 502.
  • The proxy could not reach the application.
  • The application answered in a way the proxy could not pass on.
yes
1015TLS HandshakeThe TLS handshake failed. Browsers never use this code: they report 1006 instead.
  • A certificate nobody trusts: self-signed, expired, or from an authority that is not in the trust store.
  • The name in the certificate does not match the address being connected to.
  • No TLS version or cipher suite in common.
  • A proxy in the middle swaps the certificate and its root is not installed.
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