1001 api network_error ai_generated partial

WebSocket close code 1001: Endpoint Unavailable

ID: api/websocket-close-code-1001-endpoint-unavailable

Also available as: JSON · Markdown · 中文
75%Fix Rate
82%Confidence
1Evidence
2023-06-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
WebSocket Protocol RFC 6455 active
Node.js ws library 8.x active
Python websockets 11.x active
Spring WebSocket 6.0+ active

Root Cause

The server is shutting down or the WebSocket endpoint is temporarily unavailable, causing an intentional close with code 1001.

generic

中文

服务器正在关闭或 WebSocket 端点暂时不可用,导致使用代码 1001 有意关闭连接。

Official Documentation

https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/statusCode

Workarounds

  1. 80% success Implement exponential backoff reconnection logic. Example in JavaScript: let delay = 1000; function reconnect() { setTimeout(() => { const ws = new WebSocket('wss://api.example.com/socket'); ws.onclose = (event) => { if (event.code === 1001) { delay = Math.min(delay * 2, 30000); reconnect(); } }; }, delay); }
    Implement exponential backoff reconnection logic. Example in JavaScript:
    let delay = 1000;
    function reconnect() {
      setTimeout(() => {
        const ws = new WebSocket('wss://api.example.com/socket');
        ws.onclose = (event) => {
          if (event.code === 1001) {
            delay = Math.min(delay * 2, 30000);
            reconnect();
          }
        };
      }, delay);
    }
  2. 85% success Check server health endpoint before reconnecting to ensure the WebSocket server is back online.
    Check server health endpoint before reconnecting to ensure the WebSocket server is back online.
  3. 75% success If using a load balancer, verify that health checks are configured correctly for the WebSocket service to avoid routing to unhealthy instances.
    If using a load balancer, verify that health checks are configured correctly for the WebSocket service to avoid routing to unhealthy instances.

中文步骤

  1. Implement exponential backoff reconnection logic. Example in JavaScript:
    let delay = 1000;
    function reconnect() {
      setTimeout(() => {
        const ws = new WebSocket('wss://api.example.com/socket');
        ws.onclose = (event) => {
          if (event.code === 1001) {
            delay = Math.min(delay * 2, 30000);
            reconnect();
          }
        };
      }, delay);
    }
  2. Check server health endpoint before reconnecting to ensure the WebSocket server is back online.
  3. If using a load balancer, verify that health checks are configured correctly for the WebSocket service to avoid routing to unhealthy instances.

Dead Ends

Common approaches that don't work:

  1. 80% fail

    The server may still be unavailable; immediate reconnection often fails repeatedly and increases server load.

  2. 90% fail

    Code 1001 is a server-initiated close due to unavailability, not a protocol version mismatch.

  3. 95% fail

    Compression settings are unrelated to server endpoint availability.