Tool Trading Bot Reliability

WebSocket Close Code Lookup

Explain WebSocket close codes from 1000 to 4999 with meanings, causes, and recovery actions for exchange streams.

15close codes
4categories

WebSocket close codes tell you why a connection terminated. In trading bots, codes 1006 (abnormal closure) and 1013 (try again later) are the most common during high volatility or rate limiting. Understanding the code helps you choose the right recovery action.

Showing 15 of 15 codes

1000normal

Normal closure

The connection closed normally. Both sides agreed to close.

Recovery: No action needed. This is a clean close.

1001normal

Going away

Server or client is going away (e.g., server shutdown, browser navigating away).

Recovery: Reconnect with exponential backoff. Check if the server is undergoing maintenance.

1002protocol

Protocol error

The endpoint received malformed data that violates the WebSocket protocol.

Recovery: Verify message framing and data format. Check for binary/text frame mismatches.

1003protocol

Unsupported data

The endpoint received data type it cannot handle (e.g., binary on a text-only endpoint).

Recovery: Ensure your messages match the expected data type (binary vs text).

1005error

No status received

Connection closed without sending a close frame.

Recovery: Treat as unexpected disconnection. Reconnect with backoff. Log the event.

1006error

Abnormal closure

Connection closed abnormally without a close frame. Common in trading bot WebSocket drops.

Recovery: This is the most common WebSocket error in trading bots. Reconnect with backoff and check for rate limiting on the endpoint.

1007protocol

Invalid frame payload data

The message payload is invalid (e.g., invalid UTF-8).

Recovery: Validate message encoding before sending. Ensure JSON is well-formed.

1008application

Policy violation

Connection rejected due to policy violation (e.g., rate limit exceeded).

Recovery: Check exchange rate limits. Reduce request frequency. Implement proper throttling.

1009application

Message too big

The message exceeds the maximum allowed frame size.

Recovery: Reduce message size or split large payloads into smaller chunks.

1011error

Internal server error

Server encountered an unexpected condition.

Recovery: Wait and retry with backoff. Check exchange status page for ongoing incidents.

1012normal

Service restart

Server is restarting. Common during exchange maintenance windows.

Recovery: Reconnect after a delay. Check exchange maintenance schedule.

1013application

Try again later

Server is temporarily overloaded. Common during high volatility.

Recovery: Wait and retry with exponential backoff. Reduce connection frequency.

4000application

Application reserved

Exchange-specific close code. Refer to exchange WebSocket documentation.

Recovery: Check the exchange-specific WebSocket documentation for the exact meaning.

4001application

Rate limited

Connection rejected due to rate limiting on the WebSocket endpoint.

Recovery: Reduce reconnection frequency. Implement exponential backoff with jitter.

4002application

Invalid auth

Authentication failed for private WebSocket channels.

Recovery: Verify API key and signature. Regenerate credentials if needed.

Related tools

Related articles

Resources and kits

Product

AlgoTrak Production Trading Bot

Production-grade crypto trading bot with robust WebSocket connection management, automatic reconnection, and rate-limit-aware stream handling. 14 strategies, full source code, and deployment guide.

View product

Frequently asked questions

What is the most common WebSocket close code in trading bots?
Code 1006 (abnormal closure) is the most common. It happens when the connection drops without a proper close frame. This often occurs during network instability, exchange maintenance, or rate limiting on the WebSocket endpoint.
How should I handle WebSocket reconnection?
Implement exponential backoff starting at 1 second, doubling up to 60 seconds, with full jitter. Track the close code: 1006 means retry, 1013 means wait longer, 4000+ means check exchange-specific rules.
What causes WebSocket rate limiting?
Exchanges limit WebSocket connections per IP and API key. Code 1013 or 4001 indicate rate limiting. Reduce connection frequency, reuse existing connections, and avoid connecting from multiple instances with the same key.

Newsletter

Weekly engineering insights

Get practical tips on AI, .NET, trading bot reliability, and building scalable systems. No spam, unsubscribe anytime.

What engineers say

The retry policy generator alone saved us from a production incident. We had exponential backoff configured wrong for months — the timeline visualization made it obvious instantly.
A

Alex R.

Senior Backend Engineer, Fintech Startup

1 / 16

WebSocket keeps dropping?

I help teams build reliable WebSocket connections for trading bots with proper reconnection strategies, rate limit handling, and monitoring.