Pirate Tok
Library guidecom.piratetok:liveSource

Java

Synchronous sessions or CompletableFuture-based connections.

Requirements

Java 25+ · Maven.

Installation

Build the public source and collect its runtime dependencies:

Shell
git clone https://github.com/PirateTok/live-java.git
mvn -f live-java/pom.xml package dependency:copy-dependencies -DskipTests

Save the quickstart as Main.java. On Linux or macOS:

Shell
javac -cp "live-java/target/*:live-java/target/dependency/*" Main.java
java -cp ".:live-java/target/*:live-java/target/dependency/*" Main

On Windows, use ; instead of : between classpath entries.

Quickstart

Replace username_here with the username of an account that is currently live.

Java
import com.piratetok.live.PirateTokClient;
import com.piratetok.live.events.EventType;

public class Main {
    public static void main(String[] args) throws Exception {
        var client = new PirateTokClient("username_here");
        client.on(EventType.CHAT, event ->
            System.out.println(event.data().get("content")));
        Runtime.getRuntime().addShutdownHook(new Thread(client::disconnect));
        client.connect();
    }
}

Connection lifecycle

connect() blocks the calling thread for the session. connectAsync() returns a future for the session lifetime. Call disconnect() from the shutdown path; do not interpret the future as a handshake-only notification.

Configuration

Java
import java.time.Duration;

var client = new PirateTokClient("username_here")
    .cdnEU()                              // EU CDN (default: global)
    .cdnUS()                              // US CDN
    .cdn("webcast-ws.eu.tiktok.com")      // custom CDN host
    .timeout(Duration.ofSeconds(15))      // HTTP timeout
    .heartbeatInterval(Duration.ofSeconds(10)) // WSS heartbeat (also sent as heartbeat_duration)
    .maxRetries(10)                       // consecutive failed attempts (reset after a 30s healthy session)
    .staleTimeout(Duration.ofSeconds(90)) // no-data timeout
    .language("en")                       // override detected language
    .region("US")                         // override detected region
    .userAgent("custom UA")               // override random UA
    .cookies("sessionid=abc; sid_tt=abc") // session cookies (18+ room info only)
    .proxy("http://user:pw@127.0.0.1:8080") // HTTP CONNECT proxy for all traffic (no SOCKS: java.net.http limit)
    .compress(false);                     // disable gzip compression for WSS payloads (trades bandwidth for CPU)

// Stop a running connection (from another thread)
client.disconnect();

Proxy notes: java.net.http only speaks HTTP CONNECT proxies, so socks5:// URLs are rejected with an IllegalArgumentException. The JDK also disables Basic proxy auth for HTTPS tunnels by default — to use user:pass@ credentials, start the JVM with -Djdk.http.auth.tunneling.disabledSchemes=.

Async (non-blocking)

Java
// Single stream
CompletableFuture<String> session = client.connectAsync();

// Multiple streams on virtual threads
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
    var futures = usernames.stream()
        .map(u -> new PirateTokClient(u).connectAsync(executor))
        .toList();
    // ...
}

Room info (optional, separate call)

Java
import com.piratetok.live.http.Api;

// Check if user is live — result.roomId(), result.anchorId() (streamer user ID)
var result = Api.checkOnline("username_here", Duration.ofSeconds(10));

// Fetch room metadata
var info = Api.fetchRoomInfo(result.roomId(), Duration.ofSeconds(10), null);

// 18+ rooms -- pass session cookies from browser DevTools
var info = Api.fetchRoomInfo(result.roomId(), Duration.ofSeconds(10),
    "sessionid=abc; sid_tt=abc");

Viewers

Every ROOM_USER_SEQ event carries the counters and the top-viewers box — no cookies needed:

Java
client.on(EventType.ROOM_USER_SEQ, e -> {
    e.data().get("viewerCount"); // in the room right now (goes up and down)
    e.data().get("totalUser");   // unique viewers over the whole stream (only grows)
    for (var c : RoomUserSeq.topViewers(e.data())) { // usually top 3, sorted by rank
        System.out.println(c.get("rank") + " " + ((Map<?, ?>) c.get("user")).get("nickname"));
    }
});

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:

Java
var room = PirateTokClient.checkOnline("username_here", Duration.ofSeconds(10));
var audience = PirateTokClient.fetchRoomAudience(room.roomId(), room.anchorId(),
    Duration.ofSeconds(10), "sessionid=abc; sid_tt=abc");
// audience.total(), audience.anonymous(), audience.viewers() (rank, score, username, followerCount, ...)

Pass null as the anchor ID to resolve it from room info (one extra request).

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.

Source and license

Public source and examples · 0BSD license