Overview
The WebSocket connection module provides low-level connection management for TikTok LIVE streams. It includes custom implementations that handle TikTok-specific requirements like signed URLs, compression, and proxy support.Classes
WebcastConnect
Custom WebSocket connection class that extends websockets.legacy.client.Connect to handle TikTok LIVE-specific requirements.
TikTokLive/client/ws/ws_connect.py:27
Constructor
Parameters
Initial response from the sign server containing WebSocket URL and parameters
Logger instance for connection logging
Base query parameters to include in the WebSocket URI
Additional string to append to the constructed WebSocket URI
Optional pre-built URI (bypasses automatic URI construction)
Additional keyword arguments passed to the parent
Connect classProperties
ws
ws_connect.py:54
ws_options
Handshake-Options header.
Location: ws_connect.py:60
Async Iterator
- Disables automatic retry mechanisms (signed URLs expire after 30 seconds)
- Handles TikTok’s status code 200 as an error (detected/blocked connection)
- Yields
(WebcastPushFrame, ProtoMessageFetchResult)tuples
None for the push frame since it comes from the initial /im/fetch response.
Location: ws_connect.py:66
WebcastProxyConnect
Extends WebcastConnect to add proxy support for WebSocket connections.
TikTokLive/client/ws/ws_connect.py:121
Constructor
Parameters
Proxy to use for the WebSocket connection. Can be:
httpx.Proxy- Automatically converted towebsockets_proxy.Proxywebsockets_proxy.Proxy- Used directly
All other parameters are passed to the parent
WebcastConnect classProxy Conversion
httpx.Proxy objects to websockets_proxy.Proxy format, preserving authentication credentials.
Location: ws_connect.py:137
Type Definitions
WebcastProxy
ws_connect.py:17
WebcastIterator
WebcastPushFrame is Optional because the first yielded item is from the initial response and not encapsulated in a push frame.
Location: ws_connect.py:24
Utility Functions
build_webcast_uri()
Parameters
Initial response containing:
push_server: WebSocket base URLcursor: Connection cursorroute_params: Server-provided routing parametersinternal_ext: Internal extension data
Additional query parameters to include (e.g.,
room_id, compress)String to append to the constructed URI
Returns
str - Complete WebSocket URI with all parameters
Raises
InitialCursorMissingError- If the cursor is missing from the responseWebsocketURLMissingError- If the push server URL or route parameters are missing
TikTokLive/client/ws/ws_utils.py:14
extract_webcast_push_frame()
WebcastPushFrame object.
Parameters
Raw byte payload from WebSocket
Logger for error reporting
Returns
WebcastPushFrame - Parsed push frame object
Location: ws_utils.py:58
extract_webcast_response_message()
ProtoMessageFetchResult from a push frame.
Parameters
Push frame to extract from
Logger for error reporting
Returns
ProtoMessageFetchResult - The extracted message result
Compression Handling
This function automatically handles gzip decompression when:- The push frame has a
compress_typeheader set to"gzip" - The payload data is gzip-compressed
Gzip compression significantly reduces bandwidth usage at the cost of minimal CPU overhead for decompression.
ws_utils.py:74
extract_websocket_options()
Handshake-Options header.
Parameters
HTTP headers from the WebSocket handshake response
Returns
dict[str, str] - Parsed options dictionary (e.g., {"ping-interval": "5.0"})
Location: ws_utils.py:108
Connection Flow
Error Handling
Status Code 200 Error
TikTok uses HTTP status code 200 to indicate a rejected/blocked WebSocket connection:Missing Required Data
The connection process validates that all required data is present:Usage Example
See Also
- WebcastWSClient - High-level WebSocket client
- Web Client - HTTP client for initial connection setup
- Errors - Error types including
WebcastBlocked200Error