Explain Codes LogoExplain Codes Logo

Websocket onerror - how to read error description?

javascript
websocket
error-handling
event-handling
Anton ShumikhinbyAnton Shumikhin·Mar 10, 2025
TLDR

To handle a WebSocket error, check the ws.readyState property as the ErrorEvent doesn't provide detailed information.

const ws = new WebSocket('wss://example.com/socket'); ws.onerror = () => { console.log(ws.readyState === WebSocket.CLOSED ? 'WebSocket closed.' : 'WebSocket error.'); };

This differentiates between a closure and a different error. Detailed info isn't provided for client-side security to prevent potentially sensitive data leakage.

Understanding WebSocket close event and error codes

When handling the WebSocket onerror event, you might find the subsequent Close event being more informative. The Close event has an event.code and event.reason carrying detailed description:

ws.onclose = (event) => { console.log('WebSocket closed with code:', event.code); console.log('Reason:', event.reason); };

Remember, ErrorCode 1006 denotes an abnormal closure without additional insights.

Breaking down common error scenarios

Server unreachable

Detect an unreachable WebSocket server by tracking the onclose event with the relevant code:

ws.onclose = (event) => { if(event.code === 1006) { console.log('You seem to love hitting unreachable servers, don’t you?'); } };

Connection drops

To minimize downtime following a connection drop, implement a reconnection attempt by listening to the 'online' window event:

window.addEventListener('online', () => { ws = new WebSocket(ws.url); // You gotta love this comeback! });

Icing your cake with error handling

Develop a fail-proof system by handling all WebSocket events - onopen, onmessage, onerror, onclose for a seamless user experience.

Your cheat sheet to WebSocket event codes

When it comes to understanding the underlying reasons for the various WebSocket event codes, the RFC6455 specification is your holy grail. Here, you will find a treasure trove articulating their meanings.

Word of caution - while it might sound tempting to hack around the reason string or employ regex to extract error codes, avoid this approach. It comes with its own set of pitfalls and it may not adhere to the RFC specification.

Harnessing WebSocket for an optimised experience

Debugging like a pro

If you're building a browser-based multiplayer game, managing WebSocket events is a game changer. Here's a quick to-do list:

  • Lace your WebSocket events with abundant logging.
  • Be on your guard with alerts for unexpected onerror events.
  • Deploy auto-reconnection strategies with backoff algorithms for smooth gaming.

Testing for resilience

Put your WebSocket implementation through its paces using stress testing tools like WebSocket Echo Server.

Prioritizing safety

The ambiguity surrounding the onerror event stems from a security design. Be wary of divulging sensitive infrastructure details through error messages. Brevity is beautiful!