Close Code Reference
All Gateway close codes are defined influxer_gateway/src/utils/constants.erl:76-89.
| Code | Name | Resumable | Description |
|---|---|---|---|
| 4000 | Unknown Error | Yes | An unknown error occurred |
| 4001 | Unknown Opcode | Yes | Invalid opcode sent |
| 4002 | Decode Error | Yes | Invalid JSON or payload |
| 4003 | Not Authenticated | Yes | Sent restricted opcode before Identify |
| 4004 | Authentication Failed | No | Invalid token |
| 4005 | Already Authenticated | Yes | Sent Identify after already authenticating |
| 4007 | Invalid Sequence | No | Resume sequence number invalid |
| 4008 | Rate Limited | No | Exceeded rate limits |
| 4009 | Session Timeout | Yes | Heartbeat timeout exceeded |
| 4010 | Invalid Shard | No | Invalid shard configuration |
| 4011 | Sharding Required | No | Session requires sharding |
| 4012 | Invalid API Version | No | Invalid Gateway version |
| 4013 | Acknowledgement Backpressure | No | Event buffer overflow |
Resumable Close Codes
These errors allow you to reconnect and resume your session.4000: Unknown Error
Cause: An unexpected internal error occurred. Recovery:- Reconnect to the Gateway
- Attempt to Resume with your session ID and last sequence
- If Resume fails, send a new Identify
4001: Unknown Opcode
Cause: You sent an opcode that doesn’t exist or isn’t supported. Recovery:- Fix your client to not send invalid opcodes
- Reconnect and Resume
- Sending opcode 13 (skipped in spec)
- Sending opcodes > 14
- Typos in opcode field
gateway_handler.erl:handle_gateway_payload/3 (unknown opcode case)
4002: Decode Error
Cause: The Gateway couldn’t parse your message. Common Reasons:- Invalid JSON syntax
- Payload exceeds 4,096 bytes
- Missing required fields (
op) - Compression failure
- Encoding mismatch
- Fix the payload format
- Ensure payloads are under 4,096 bytes
- Reconnect and Resume
gateway_handler.erl:handle_incoming_data/2
4003: Not Authenticated
Cause: You sent an opcode that requires authentication before sending Identify. Restricted Opcodes:- Opcode 3: Presence Update
- Opcode 4: Voice State Update
- Opcode 8: Request Guild Members
- Opcode 14: Lazy Request
- Reconnect
- Send Identify or Resume
- Wait for Ready/RESUMED
- Then send other opcodes
gateway_handler.erl:handle_gateway_payload/3
4005: Already Authenticated
Cause: You sent Identify after already authenticating. How This Happens:- Sent Identify twice
- Sent Identify after Resume succeeded
- Reconnect
- Send only ONE Identify or Resume per connection
gateway_handler.erl:handle_gateway_payload/3 (identify when session_pid exists)
4009: Session Timeout
Cause: You didn’t respond to heartbeats within 45 seconds. Heartbeat Flow:- Gateway sends Heartbeat (op 1) every ~13.75s
- Client responds with Heartbeat ACK
- Gateway waits up to 45s for ACK
- If no ACK, closes with 4009
- Ensure heartbeat timer is running
- Respond to ALL heartbeat requests
- Reconnect and Resume
gateway_handler.erl:handle_heartbeat_state/6
Non-Resumable Close Codes
These errors require starting a fresh session with Identify.4004: Authentication Failed
Cause: The token you provided is invalid. Common Reasons:- Token is malformed
- Token has expired
- Token was revoked
- Wrong token type (bot vs user)
- Verify your token is correct
- Obtain a new token if expired
- Reconnect and send Identify with valid token
- Do NOT attempt Resume
This close code is NOT resumable. Do not attempt to Resume.
gateway_handler.erl:start_session/3 (invalid_token case)
4007: Invalid Sequence
Cause: The sequence number in your Resume request is invalid. Common Reasons:- Sequence is too old (session expired)
- Sequence is in the future (shouldn’t happen)
- Session data was lost server-side
- Do NOT attempt Resume again
- Reconnect and send a new Identify
- Store sequence numbers more reliably
This close code is NOT resumable. Start a fresh session.
gateway_handler.erl:handle_resume_invalid_seq/1
4008: Rate Limited
Cause: You exceeded Gateway rate limits. Rate Limits:- General: 120 events per 60 seconds
- Request Guild Members: 3 per 10 seconds
- Voice State Update: 10 per second (queued beyond)
- Wait at least 60 seconds before reconnecting
- Review your code to reduce event frequency
- Reconnect and send a new Identify
- Do NOT attempt Resume
This close code is NOT resumable. Wait before reconnecting.
gateway_handler.erl:check_rate_limit/2
4012: Invalid API Version
Cause: You didn’t specifyv=1 in the connection URL.
Valid URL:
- Update your connection URL to include
v=1 - Reconnect
- Send Identify
This close code is NOT resumable. Fix your URL.
gateway_handler.erl:websocket_init/1 (version check)
4013: Acknowledgement Backpressure
Cause: Your client isn’t processing events fast enough, causing buffer overflow. Buffer Limits:- Event ACK buffer: 4,096 events
- Reaction buffer: 512 events
- Pending presence buffer: 2,048 events
- Client is blocking/hanging
- Network congestion
- Not acknowledging events
- Fix your client to process events faster
- Ensure event handlers are non-blocking
- Reconnect and send Identify (cannot Resume)
This close code is NOT resumable. Optimize your client.
session_dispatch.erl:report_buffer_overflow/4
Reconnection Strategy
Implement this flowchart for robust reconnection:Implementation Example
Monitoring
Track these metrics to detect issues:Close Code Distribution
Monitor which close codes occur most frequently
Resume Success Rate
Track Resume success vs failure rate
Reconnection Time
Measure time to reconnect and resume
Heartbeat Latency
Monitor round-trip time for heartbeats
Summary
Resumable Codes (4000, 4001, 4002, 4003, 4005, 4009)
Resumable Codes (4000, 4001, 4002, 4003, 4005, 4009)
- Reconnect immediately (or with short delay)
- Attempt Resume with session ID and sequence
- Fall back to Identify if Resume fails
Non-Resumable Codes (4004, 4007, 4010, 4011, 4012)
Non-Resumable Codes (4004, 4007, 4010, 4011, 4012)
- Clear session ID
- Reconnect with delay
- Send fresh Identify
- Fix underlying issue
Rate Limited (4008)
Rate Limited (4008)
- Wait at least 60 seconds
- Clear session ID
- Reduce event frequency
- Send Identify after delay
Backpressure (4013)
Backpressure (4013)
- Optimize client event processing
- Clear session ID
- Fix blocking code
- Send Identify after fixes