Skip to content

connect

Connect to the Webcast server & iterate over response messages.

Method of WebcastWSClient.

Connect to the Webcast server & iterate over response messages.

--- Message 1 ---

The iterator exits normally when the connection is closed with close code 1000 (OK) or 1001 (going away) or without a close code. It raises a ~websockets.exceptions.ConnectionClosedError when the connection is closed with any other code.

--- Message 2 ---

DEVELOP SANITY NOTE:

When ping_timeout is set (i.e. not None), the client waits for a pong for N seconds. TikTok DO NOT SEND pongs back. Unfortunately the websockets client after N seconds assumes the server is dead. It then throws the following infamous exception:

websockets.exceptions.ConnectionClosedError: sent 1011 (unexpected error) keepalive ping timeout; no close frame received

If you set ping_timeout to None, it doesn’t wait for a pong. Perfect, since TikTok don’t send them.

--- Parameters —

async def connect(
room_id: int,
cookies: httpx.Cookies,
user_agent: str,
initial_webcast_response: ProtoMessageFetchResult,
process_connect_events: bool = True,
compress_ws_events: bool = True,
) -> AsyncIterator[ProtoMessageFetchResult]
  • room_id (int) — The room ID to connect to
  • cookies (httpx.Cookies) — The cookies to pass to the WebSocket connection
  • user_agent (str) — The user agent to pass to the WebSocket connection
  • initial_webcast_response (ProtoMessageFetchResult) — The Initial ProtoMessageFetchResult from the sign server - NOT a PushFrame
  • process_connect_events (bool, default True) — Whether to process the initial events sent in the first fetch
  • compress_ws_events (bool, default True) — Whether to ask TikTok to gzip the WebSocket events

AsyncIterator[ProtoMessageFetchResult] — Yields ProtoMessageFetchResultMessage, the messages within ProtoMessageFetchResult.messages

from TikTokLive.client.ws.ws_client import WebcastWSClient

TikTokLive/client/ws/ws_client.py:171