Pirate Tok
Library guidepiratetok-live-luaSource

Lua

A polling API for game loops and embedded integrations.

Requirements

Lua 5.1+ or LuaJIT · native sockets.

Installation

Shell
luarocks install piratetok-live-lua

Save the quickstart as reader.lua and run lua reader.lua. Required native modules include LuaSocket, LuaSec, lua-protobuf, lua-zlib and lua-cjson.

Quickstart

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

Lua
local PirateTok = require "piratetok"
local client = PirateTok.builder("username_here"):build()

client:on("chat", function(message)
    local name = message.user and message.user.nickname or "?"
    print(name .. ": " .. (message.comment or ""))
end)

client:run()

Connection lifecycle

client:run() drives a blocking event loop. For a game engine, call client:poll() repeatedly from the update loop instead. Polling also drives heartbeats and reconnect handling.

Configuration

Lua
local client = PirateTok.builder("username_here")
    :cdn("eu")                  -- "eu" / "us" / "global" (default)
    :timeout(15)                -- HTTP timeout in seconds (default 10)
    :heartbeat_interval(10)     -- seconds between heartbeats (default 10)
    :stale_timeout(90)          -- reconnect after N seconds of silence (default 60)
    :max_retries(10)            -- consecutive failed reconnects before giving up (default 5)
    :proxy("http://user:pass@host:port") -- HTTP CONNECT proxy for HTTP + WSS (Basic auth optional; SOCKS not supported)
    :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)
    :build()

Events

Event Callback data
chat .user, .comment
gift .user, .gift_details, .repeat_count, .repeat_end, .combo_count
like .user, .count, .total
join .user (sub-routed from MemberMessage)
follow .user (sub-routed from SocialMessage)
share .user (sub-routed from SocialMessage)
room_user_seq .viewer_count, .total_user, .anonymous, .pop_str, .ranks_list (use PirateTok.top_viewers(data))
live_ended .reason (sub-routed from ControlMessage)
connected .room_id
reconnecting .attempt, .max_retries, .delay_secs, .reason
disconnected .reason
error error table (use PirateTok.errors.format(err))
unknown .method, .payload (raw bytes for unhandled types)

Plus 58 more decoded types: emote_chat, poll, envelope, rank_update, link_mic_battle, etc.

Room info (optional, separate call)

Lua
-- Normal rooms — no cookies needed
local info, err = PirateTok.fetch_room_info("ROOM_ID")

-- 18+ rooms — pass session cookies from browser DevTools
local info, err = PirateTok.fetch_room_info("ROOM_ID", 10, "sessionid=abc; sid_tt=abc")

Top viewers (WSS, no cookies)

Lua
client:on("room_user_seq", function(seq)
    for _, c in ipairs(PirateTok.top_viewers(seq)) do
        print(c.rank, c.user.nickname, c.score)
    end
end)

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 SessionRequired.

Lua
local live = PirateTok.check_online("username_here")
local aud, err = PirateTok.fetch_room_audience(
    live.room_id, live.anchor_id, "sessionid=abc; sid_tt=abc")
if aud then
    print(aud.total, aud.anonymous)
    for _, v in ipairs(aud.viewers) do print(v.rank, v.username, v.score) end
end

anchor_id may be nil — it is then resolved via room info (one extra request).

Gift streaks

Lua
client:on("gift", function(msg)
    local events = PirateTok.events
    if events.is_combo_gift(msg) then
        if events.is_streak_over(msg) then
            print("x" .. msg.repeat_count .. " = " .. events.diamond_total(msg) .. " diamonds")
        end
    else
        print(events.diamond_total(msg) .. " diamonds")
    end
end)

Game engine usage (Love2D)

Poll-based API — call client:poll() in your game loop instead of client:run():

Lua
local PirateTok = require "piratetok"
local messages = {}

local client = PirateTok.builder("username_here"):build()
client:on("chat", function(msg)
    local name = msg.user and msg.user.nickname or "?"
    table.insert(messages, name .. ": " .. (msg.comment or ""))
end)
client:connect()

function love.update(dt)
    client:poll()  -- non-blocking, reads available frames
end

function love.draw()
    for i, line in ipairs(messages) do
        love.graphics.print(line, 10, i * 20)
    end
end

Runtime compatibility

Requires Lua 5.1+ (including LuaJIT) with unrestricted system access (raw TCP sockets). Works in:

  • Love2D — poll-based API designed for game loops
  • Defold — with native extension packaging for C deps
  • Standalone LuaJIT / Lua — scripts, bots, monitoring tools

Does not work in sandboxed Lua environments (Roblox, Neovim, FiveM, OpenResty) — these restrict socket access and/or use incompatible Lua variants.

TLS

Every HTTPS / WSS connection verifies the server certificate chain and the hostname (subjectAltName). The CA bundle comes from the system trust store: $SSL_CERT_FILE / $SSL_CERT_DIR if set, otherwise the standard locations on Linux, the BSDs and macOS (/etc/ssl/certs/ca-certificates.crt, /etc/pki/tls/certs/ca-bundle.crt, /etc/ssl/cert.pem, …). Windows has no file-based store luasec can read — set SSL_CERT_FILE to a PEM bundle (e.g. the one shipped with curl or Git for Windows); without it connections fail with "no CA trust store found". To pin a custom bundle in code: require("piratetok.tls").ca_file = "/path/ca.pem".

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