C / FFI
A C ABI with background callbacks and JSON event payloads.
Requirements
Rust toolchain to build · C compiler to integrate.
Installation
Build the shared library from source:
git clone https://github.com/PirateTok/live-c.git
cargo build --manifest-path live-c/Cargo.toml --release
Save the quickstart as reader.c. Compile and run on Linux:
cc reader.c -I live-c/include -L live-c/target/release -lpiratetok -o reader
LD_LIBRARY_PATH=live-c/target/release ./reader
The shared-library filename is platform-specific: .so on Linux, .dll on Windows and .dylib on macOS.
Quickstart
Replace username_here with the username of an account that is currently live.
#include "piratetok.h"
#include <stdio.h>
static void on_event(PirateTokEventType type, const char *json,
size_t length, void *user_data) {
(void)user_data;
if (type == CYCLED_EVENT_CHAT) {
fwrite(json, 1, length, stdout);
fputc('\n', stdout);
}
}
int main(void) {
PirateTokRuntime *runtime = piratetok_init();
if (!runtime) return 1;
PirateTokClient *client = piratetok_client_new(runtime, "username_here");
if (!client) { piratetok_shutdown(runtime); return 1; }
PirateTokError status = piratetok_connect(client, on_event, NULL);
if (status == PIRATETOK_OK) {
puts("Press Enter to stop.");
getchar();
piratetok_disconnect(client);
}
piratetok_client_free(client);
piratetok_shutdown(runtime);
return status == PIRATETOK_OK ? 0 : 1;
}
Connection lifecycle
Callbacks execute on a background thread. The JSON pointer is valid only during the callback; copy it before retaining it. Disconnect before freeing the client, and free every client before shutting down the runtime. Release strings returned by utility functions with piratetok_string_free().
API reference
Lifecycle:
piratetok_init()-- create async runtime (call once)piratetok_client_new(rt, username)-- create client for a TikTok userpiratetok_connect(client, callback, user_data)-- start streaming events (non-blocking)piratetok_disconnect(client)-- stop connection (blocking)piratetok_client_free(client)-- free client resourcespiratetok_shutdown(rt)-- shut down runtime
Configuration (call before connect):
piratetok_client_set_cdn(c, PIRATETOK_CDN_EU)piratetok_client_set_timeout_ms(c, 15000)piratetok_client_set_max_retries(c, 10)piratetok_client_set_heartbeat_ms(c, 10000)/piratetok_client_set_stale_timeout_ms(c, 60000)piratetok_client_set_proxy(c, "socks5://127.0.0.1:1080")piratetok_client_set_user_agent(c, "...")/piratetok_client_set_cookies(c, "sessionid=...; sid_tt=...")piratetok_client_set_language(c, "de")/piratetok_client_set_region(c, "DE")(default: system locale)piratetok_client_set_compress(c, 0)(default 1)
Utilities:
piratetok_check_online(rt, username, &room_id, &anchor_id)-- check if user is live (anchor_idmay beNULL)piratetok_fetch_room_info(rt, room_id, cookies, &json)-- fetch room metadata (cookies only for 18+)piratetok_fetch_room_audience(rt, room_id, anchor_id, cookies, &json)-- full named viewer roster (login-gated,PIRATETOK_ERR_SESSION_REQUIREDwithout cookies)piratetok_fetch_profile(rt, username, &json)-- profile metadata + HD avatars (cached per runtime)piratetok_string_free(s)-- free strings returned by utilities
Events arrive as null-terminated JSON on a background thread via the callback. PirateTokEventType has an id for every event the Rust lib decodes (64 typed + lifecycle + Unknown). Every event JSON carries type, type_id and, for messages, payload_b64 (the full protobuf, so any field is reachable); common events also carry decoded fields. ROOM_USER_SEQ includes top_viewers (sorted by rank), GIFT includes streak (per-event deltas), LIKE includes like_stats (monotonic totals). Users carry badges/levels and follower counts. Absent optional fields are null.
Reconnects reuse the ttwid and only rotate it on DEVICE_BLOCKED or a connection that died within 30 s; a missing ttwid cookie is retried 8×750 ms before it counts as a failed attempt; max_retries counts consecutive failures (a 30 s healthy session resets it).
Protocol reference
The basic LIVE connection uses an anonymous ttwid cookie. Session cookies may be needed for restricted room metadata or the full audience roster. These are separate calls from the event stream.
- WebSocket contract
- Room metadata
- Audience endpoint
- Authentication and connection lifecycle
- Troubleshooting
Source and license
Public source and examples · 0BSD license