Pirate Tok
Docs/Getting started/Connection lifecycle

Connection lifecycle

Understand what happens between a username and a stream of events, and how to integrate that session into your application's lifecycle.

From username to events

  1. Resolve the account to a room ID through API Live User Room.
  2. Obtain an anonymous ttwid cookie.
  3. Open the direct LIVE WebSocket.
  4. Send heartbeat and room-entry frames, decode incoming messages, and acknowledge messages when required.
  5. Reconnect according to the library's retry and stale-connection settings.

The library handles the wire protocol. Your application handles events, connection status and shutdown.

A connection method is not always a handshake promise

Library Connection API Lifetime
JavaScript await client.connect() Resolves after the first successful handshake; events continue in the background.
Python await client.connect() Runs the session until disconnection or retry exhaustion.
Rust builder.connect().await Resolves the room and returns an event stream; the supervisor connects in the background.
Go client.Connect(ctx) Resolves the room and returns an event channel.
C# await client.RunAsync() Runs until final disconnection or cancellation.
Java connect() / connectAsync() Represents the whole session, synchronously or through a future.
Dart await client.connect() Runs the asynchronous session.
Elixir PirateTok.Live.connect/2 Starts a process that sends event messages to the caller.
Lua client:run() / client:poll() Either drives the loop or lets the application drive it.
C / FFI piratetok_connect() Starts background callbacks; the caller owns runtime and client handles.
PowerShell Start-TikTokLive / Receive-TikTokEvent Drives a callback loop or explicitly polls a client.
Shell pt_connect Runs a blocking callback loop.

Register handlers before connecting

Startup and room-entry events may arrive immediately. Attach the handlers you need before starting the client. In polling APIs, keep polling so the client can process traffic and connection maintenance.

Configure retries deliberately

The libraries expose a retry budget and a stale timeout. Use the language guide for the exact parameter names and units. Python timeouts use seconds; JavaScript and Elixir commonly use milliseconds; Rust, Go, Java, C# and Dart expose duration types in their APIs.

A connection being re-established is different from a session ending permanently. Keep application state for a reconnect, and release resources when the session has actually stopped.

Shut down explicitly

  • Disconnect callback-based clients from your application's shutdown path.
  • Cancel Go contexts when the owning operation ends.
  • Drop the Rust stream when you no longer want the supervisor to run.
  • In C, disconnect and free clients before shutting down the runtime. Copy callback data before keeping it beyond the callback.
  • In PowerShell, use try / finally around the event loop and call Disconnect-TikTokLive in finally.

Keep metadata failures separate

An age-restricted room-info response does not, by itself, prove that the anonymous event stream cannot connect. The room metadata endpoint and the WebSocket handshake have different contracts.

The full audience roster is also a separate, authenticated HTTP operation. It is distinct from viewer counters and the top-viewers information delivered by LIVE events.

Next steps