C#
Typed event callbacks for .NET applications and Unity.
Requirements
.NET SDK 8+ for the sample · netstandard2.0 library.
Installation
Use a project reference to build directly from the public source:
git clone https://github.com/PirateTok/live-cs.git
dotnet new console -n LiveReader
dotnet add LiveReader/LiveReader.csproj reference live-cs/src/TikTokLive/TikTokLive.csproj
Replace LiveReader/Program.cs with the quickstart, then run dotnet run --project LiveReader.
Quickstart
Replace username_here with the username of an account that is currently live.
using System;
using System.Threading;
using TikTokLive;
var client = new TikTokLiveClient("username_here");
client.OnChat += message =>
Console.WriteLine($"{message.User?.Nickname ?? "?"}: {message.Comment}");
await client.RunAsync();
Connection lifecycle
RunAsync() represents the entire session and completes after cancellation or the final disconnection. Register event handlers before calling it. Use the client’s cancellation-token overload when integrating with application shutdown.
Configuration
using System;
using TikTokLive;
var client = new TikTokLiveClient("username_here")
.CdnEu()
.Timeout(TimeSpan.FromSeconds(15))
.MaxRetries(10)
.StaleTimeout(TimeSpan.FromSeconds(90));
Builder methods
| Method | Default | Description |
|---|---|---|
.Cdn(host) |
webcast-ws.tiktok.com |
Set custom CDN hostname |
.CdnEu() |
— | Shorthand for EU CDN (webcast-ws.eu.tiktok.com) |
.CdnUs() |
— | Shorthand for US CDN (webcast-ws.us.tiktok.com) |
.Timeout(TimeSpan) |
10s | HTTP request timeout for ttwid fetch, online check, room info |
.HeartbeatInterval(TimeSpan) |
10s | Interval between WSS heartbeat frames (also sent as heartbeat_duration) |
.MaxRetries(int) |
5 | Maximum consecutive failed attempts before giving up (reset after a 30s healthy session) |
.StaleTimeout(TimeSpan) |
60s | Close and reconnect if no data arrives within this window |
.Proxy(string) |
none | HTTP/HTTPS/SOCKS5 proxy URL — applies to all HTTP requests and WSS |
.Proxy(IWebProxy) |
none | Same, but accepts a System.Net.IWebProxy instance directly |
.UserAgent(string) |
random | Override the random UA pool with a fixed user agent |
.Cookies(string) |
none | Append session cookies alongside ttwid in WSS cookie header ("sessionid=xxx; sid_tt=xxx") |
.Language(string) |
system | Override language sent in WSS URL params (auto-detected from system locale) |
.Region(string) |
system | Override region sent in WSS URL params (auto-detected from system locale) |
.Compress(bool) |
true |
Disable gzip compression for WSS payloads (trades bandwidth for CPU) |
Room info (optional, separate call)
// Normal rooms
var info = await TikTokLiveClient.FetchRoomInfoAsync(
"ROOM_ID",
TimeSpan.FromSeconds(10));
// 18+ rooms — pass session cookies from browser DevTools
var info = await TikTokLiveClient.FetchRoomInfoAsync(
"ROOM_ID",
TimeSpan.FromSeconds(10),
cookies: "sessionid=abc; sid_tt=abc");
Viewers
The top-viewers box (usually top 3) rides on every RoomUserSeq event — no cookies needed:
client.OnRoomUserSeq += msg =>
{
foreach (var c in msg.TopViewers())
Console.WriteLine($"#{c.Rank} {c.User?.Nickname} ({c.Score})");
};
The full audience roster is a separate call. TikTok gates it behind a login, so session cookies are
required for this call only — without them it throws SessionRequiredException:
var room = await TikTokLiveClient.CheckOnlineAsync("username_here", TimeSpan.FromSeconds(10));
var audience = await TikTokLiveClient.FetchRoomAudienceAsync(
room.RoomId, room.AnchorId, TimeSpan.FromSeconds(10),
cookies: "sessionid=abc; sid_tt=abc");
// audience.Total, audience.Anonymous, audience.Viewers (rank, score, username, follower count, ...)
Pass null as the anchor ID to resolve it from room info (one extra request).
Known gaps
None currently tracked.
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