Overview
TheWebcastWSClient class is responsible for managing WebSocket connections to TikTok LIVE streams. It handles connection management, message sending/receiving, acknowledgments, and heartbeat pings to keep the connection alive.
Class Definition
Location
TikTokLive/client/ws/ws_client.py:17
Constructor
Parameters
Optional dictionary of keyword arguments to override WebSocket connection settings. These kwargs are passed to the underlying
websockets library.Common Options:subprotocols: List of WebSocket subprotocols (default:["echo-protocol"])extra_headers: Dictionary of additional HTTP headers to send during handshakebase_uri_params: Additional query parameters for the WebSocket URIbase_uri_append_str: String to append to the WebSocket URIproxy_conn_timeout: Proxy connection timeout in seconds (default: 10.0 when using proxy)ping_interval: Custom ping interval (handled internally by TikTok)ping_timeout: Custom ping timeout (set toNoneinternally as TikTok doesn’t send pongs)
Optional proxy for the WebSocket connection. Can be either:
httpx.Proxy- Automatically converted to websockets proxy formatwebsockets_proxy.Proxy- Used directly
Properties
ws
None if not connected.
Returns: Optional[WebSocketClientProtocol]
Location: ws_client.py:42
connected
bool - True if connected, False otherwise
Location: ws_client.py:58
Methods
connect()
Parameters
The TikTok LIVE room ID to connect to
HTTP cookies to include in the WebSocket handshake (includes session authentication)
User-Agent string to send in the WebSocket handshake headers
The initial response from the sign server containing WebSocket connection details
Whether to process and yield events from the initial response
Whether to request gzip compression for WebSocket events (reduces bandwidth usage)
Returns
AsyncIterator[ProtoMessageFetchResult] - Async iterator yielding message results from the stream
Important Notes
The iterator exits normally when the connection closes with code 1000 (OK) or 1001 (going away). Other close codes raise
ConnectionClosedError.ws_client.py:167
send()
Parameters
Message to send. Can be raw bytes or a protobuf Message object (will be serialized automatically)
ws_client.py:69
send_ack()
Parameters
The message result to acknowledge
The push frame containing the message (provides log_id for acknowledgment)
ws_client.py:90
disconnect()
ws_client.py:120
switch_rooms()
im_enter_room message and restarts the ping loop.
Parameters
The new room ID to switch to
ws_client.py:296
get_ws_cookie_string()
Parameters
HTTP cookies to format for WebSocket handshake
Returns
str - Cookie string formatted for WebSocket headers
This method logs differently for authenticated (with sessionid) vs anonymous sessions, with session IDs redacted in logs for security.
ws_client.py:132
Internal Methods
restart_ping_loop()
ws_client.py:284
_ping_loop_fn()
ws_client.py:319
Constants
DEFAULT_PING_INTERVAL
Handshake-Options header.
Location: ws_client.py:20
Usage Example
See Also
- WebSocket Connection - Connection internals and utilities
- TikTokLiveClient - Main client that uses WebcastWSClient