Pirate Tok
Library guidepiratetok_liveSource

Elixir

GenServer connections that deliver messages to a process.

Requirements

Elixir 1.15+.

Installation

Use the public Git repository for the API described here. Add this dependency in mix.exs:

Elixir
defp deps do
  [{:piratetok_live, github: "PirateTok/live-ex", branch: "main"}]
end

Run mix deps.get, then execute the quickstart with mix run reader.exs.

Quickstart

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

Elixir
defmodule LiveReader do
  def run do
    {:ok, _pid} = PirateTok.Live.connect("username_here")
    listen()
  end

  defp listen do
    receive do
      {:tiktok_live, :disconnected, _} ->
        :ok
      {:tiktok_live, :chat, message} ->
        IO.puts(message.comment)
        listen()
      _event ->
        listen()
    end
  end
end

LiveReader.run()

Connection lifecycle

connect/2 starts a connection process and sends events to the calling process as {:tiktok_live, type, data}. Keep receiving messages to consume the stream. Call PirateTok.Live.disconnect(pid) to stop a connection.

Configuration

Elixir
{:ok, pid} = PirateTok.Live.connect("username_here",
  cdn: :eu,                  # :eu / :us / :global (default)
  timeout: 15_000,           # HTTP timeout in ms (default 10_000)
  heartbeat_interval: 10_000, # ms between heartbeats (default 10_000)
  stale_timeout: 90_000,     # reconnect after N ms of silence (default 60_000)
  max_retries: 10,           # consecutive failed reconnects before giving up (default 5)
  proxy: "http://user:pass@host:port", # HTTP CONNECT proxy, Basic auth optional (SOCKS rejected)
  compress: false,           # disable gzip compression for WSS payloads (default true)
  user_agent: "Mozilla/...", # override random UA rotation with a fixed user-agent
  cookies: "sessionid=xxx; sid_tt=xxx", # session cookies for 18+ room info
  language: "en",            # override detected system language (two-letter code)
  region: "US"               # override detected system region (two-letter code)
)

Room info (optional, separate call)

Elixir
# Check if user is live -- anchor_id is the streamer's user id
{:ok, %{room_id: room_id, anchor_id: anchor_id}} = PirateTok.Live.check_online("username_here")

# Fetch room metadata (title, viewers, stream URLs)
{:ok, info} = PirateTok.Live.fetch_room_info(room_id)

# 18+ rooms -- pass session cookies from browser DevTools
{:ok, info} = PirateTok.Live.fetch_room_info(room_id,
  cookies: "sessionid=abc; sid_tt=abc")

check_online/2 errors: :user_not_found, :host_not_online, :api_error (statusCode in the message), :tiktok_blocked (HTTP 403/429, empty or non-JSON body).

Top viewers (WSS, no cookies)

Elixir
{:tiktok_live, :room_user_seq, seq} ->
  for c <- PirateTok.Live.top_viewers(seq), do: IO.puts("#{c.rank} #{c.user.nickname} #{c.score}")

:room_user_seq carries viewer_count, total_user, anonymous, pop_str, ranks_list, seats_list.

Audience roster (optional, login-gated)

The full viewer list behind the web viewer panel. Session cookies are required for this call only -- without them you get an error of type :session_required.

Elixir
{:ok, aud} = PirateTok.Live.fetch_room_audience(room_id, anchor_id,
  cookies: "sessionid=abc; sid_tt=abc")
# aud.total, aud.anonymous, aud.viewers (rank, score, user_id, username, nickname, ...)

Pass nil as anchor_id to resolve it via 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