What errors can WebSocket emit?

Asked by At

Is there a list of all possible error events a js websocket client can emit? Are the errors spec'ed or are they implementation dependent?

webSocket.on('error', (event) => ...) // What can event be?

2 Answers

Wisam398 On

here is a list of Websocket errors codes that you might receive. websocket-close-codes

Most likely you'll receive 1006 in case of an exception

Myst On

Browser-side error events are actually related to "close codes" used by the WebSocket protocol, as detailed in section 11.7 to the RFC.

You can find the registered WebSocket closure codes here.

In addition to server-side specified errors, some parsing errors and protocol errors are also emitted by the client (such as UTF-8 requirements)... which are often mapped to a closure code (UTF-8 is mapped to code 1003).

AFAIK, these closure error codes are actually sent to the onclose callback, as part of the close event. (i.e., close_event.code). However, according to MDN when the closure isn't normal (code 1000), the onerror callback is also called.

Personally I've never tested or coded anything with these error-codes, since they are unreliable and optional:

When closing an established connection (e.g., when sending a Close frame, after the opening handshake has completed), an endpoint MAY indicate a reason for closure.

Exposing these "error codes" is optional for a reason. Sending error codes from a server to a client / application could (potentially) expose security vulnerabilities.