Skip to main content

Close Codes

When a WebSocket connection closes, it includes a numeric code and a reason string.

Standard Codes

CodeNameDescription
1000Normal ClosureClean disconnect — stream ended or you closed the connection
1001Going AwayServer shutting down or stream ended
1006Abnormal ClosureConnection dropped without a close frame (network issue)
1008Policy ViolationViolated rate limits or usage policy
1011Internal ErrorServer-side error — reconnect with backoff

TikTool Custom Codes

CodeNameDescription
4000Stream OfflineThe TikTok user is not currently live
4001Stream EndedThe live stream ended while you were connected
4002Rate LimitedToo many connections — wait and retry
4003Authentication FailedInvalid API key or expired JWT token
4004User Not FoundThe TikTok username doesn’t exist
4005Tier LimitConnection limit reached for your tier
4006Duration LimitMaximum connection duration reached (Sandbox: 5 min, Basic: 30 min)
4007Server OverloadedServer at capacity — retry with exponential backoff

Handling Close Codes

ws.on('close', (code, reason) => {
  const reasonStr = reason.toString();

  switch (code) {
    case 4000:
    case 4001:
      console.log('Stream offline/ended — stop reconnecting');
      break;
    case 4003:
      console.error('Auth failed — check your API key');
      break;
    case 4005:
    case 4006:
      console.warn('Tier limit — upgrade your plan');
      break;
    default:
      // Reconnect with exponential backoff
      setTimeout(connect, reconnectDelay);
      reconnectDelay = Math.min(reconnectDelay * 2, 30000);
  }
});
Don’t reconnect on codes 4000, 4001, 4003, or 4004 — these indicate permanent conditions that won’t change on retry.