People search "twitch chat logs" about 6,600 times a month in the US, and most of them hit the same wall: Twitch has no API that returns chat from the past. Helix doesn't have one, and neither does EventSub. If a message isn't captured while it scrolls by, it's gone for you. So a chat log is something you build going forward: connect, listen, store.
This post builds twitchlog, a small Go program that does exactly that:
-
logconnects to Twitch chat over its IRC WebSocket, joins channels, and writes every message to SQLite, along with subs, raids, timeouts, bans, and room settings -
searchruns full-text queries (FTS5) across everything you've logged -
exportprints a plain-text log for one channel and day, or for one chatter -
statssummarizes a channel: messages per minute, unique chatters, subscriber share, moderation, top Twitch emotes, events -
importrebuilds the database from the raw IRC log, so the raw log is your source of truth -
live(optional) checks which channels are live before you join them
It's five Go files and two dependencies: github.com/coder/websocket for the WebSocket and modernc.org/sqlite, a pure-Go SQLite (no cgo). Everything below ran on November 20, 2026 with Go 1.27.3 on a Linux VM. I logged #xqc and #kaicenat for about 24 minutes in total (12,443 messages). Output blocks are pasted from those runs, except that I replaced chatters' usernames with viewer_NN placeholders.
Read this first. Twitch's terms prohibit scraping. The Twitch Terms of Service (last modified 08/12/2026) say you may not "access any website, server, software application, or other computer resource owned, used, and/or licensed by Twitch, including but not limited to the Twitch Services, by means of any robot, spider, scraper, crawler, or other automated means for any purpose", and the license section excludes "use of any data mining, robots, or similar data gathering or extraction methods". Read those clauses and think about the legal risk for your use case before you run anything here. Prefer the official routes: Twitch documents IRC and EventSub as its chat interfaces, and for new projects it recommends EventSub (
channel.chat.message) with a user access token that has theuser:read:chatscope. The anonymousjustinfanlogin used below is not documented. Chat is also personal data: usernames next to what people said. Keep logs only as long as you need them, don't publish logs of individual people, and check privacy law (GDPR and similar) if you store them. The code redacts messages that moderators delete or that belong to timed-out and banned users, because Twitch removes them from view too.
What you can and can't get
| What | How | Notes |
|---|---|---|
| Live chat, from now on | IRC over WebSocket (wss://irc-ws.chat.twitch.tv:443) or EventSub | This post uses IRC; EventSub needs a user token |
| Chat from before you connected | No API | Helix and EventSub only deliver new messages |
| Chat replay on VODs | Only through the website's private GraphQL | Not covered here |
| Who is in chat | Helix Get Chatters, needs a moderator token | Anonymous IRC only lists you in NAMES |
| Timeouts, bans, deleted messages | CLEARCHAT and CLEARMSG events | You only see them if you're connected at the time |
| 7TV, BTTV, FFZ emotes | Not in the IRC tags | They arrive as plain words |
Before writing a parser, I tested which transport the VM could reach. The raw IRC ports didn't work from that network: TLS on port 6697 ended with unexpected eof, and a plain connection to 6667 got HTTP/1.1 400 Bad Request from an HTTP proxy. The WebSocket upgrade on port 443 worked first try:
HTTP/1.1 101 Switching Protocols Connection: upgrade Upgrade: websocket That's the main reason to use WebSockets: it's ordinary HTTPS traffic, so it gets through proxies and firewalls that block IRC ports. Twitch shut down the unencrypted ws:// endpoint on port 80 in August 2025, so use wss://.
Setup
mkdir twitchlog && cd twitchlog go mod init twitchlog go get github.com/coder/websocket@v1.8.15 modernc.org/sqlite@v1.61.1 The program is split into irc.go (parsing), conn.go (the connection), store.go (SQLite), main.go (commands), and stats.go (the report and the live check). All five are below in full, so you can copy them into one folder and go build.
Step 1: Parse Twitch's IRC lines
Twitch sends IRC with IRCv3 tags in front. A chat message looks like this (shortened, username replaced):
@badge-info=;badges=bronze-chain/1;color=#8A2BE2;display-name=viewer_01;emotes=;first-msg=0;id=00000000-0000-4000-8000-000000000001;mod=0;returning-chatter=0;room-id=71092938;subscriber=0;tmi-sent-ts=1795147987206;user-id=100000001 :viewer_01!viewer_01@viewer_01.tmi.twitch.tv PRIVMSG #xqc :SCHIZO The tags carry everything useful: a stable message id, the numeric user-id (logins can change, IDs can't), badge-info with subscription months, emotes with character positions, bits, first-msg, reply-parent-msg-id, and the server timestamp tmi-sent-ts in milliseconds. Tag values are escaped (\s is a space, \: is a semicolon), so unescape them. System messages are full of escapes: a raid arrives as system-msg=1\sraiders\sfrom\s...\shave\sjoined!.
irc.go:
package main import ( "strings" ) // Message is one parsed IRC line from Twitch, IRCv3 tags included. type Message struct { Raw string Tags map[string]string Prefix string // "nick!user@host" or "tmi.twitch.tv" Command string // PRIVMSG, USERNOTICE, CLEARMSG, PING, 001, ... Params []string } // Nick returns the login part of the prefix ("" for server messages). func (m Message) Nick() string { if i := strings.IndexByte(m.Prefix, '!'); i > 0 { return m.Prefix[:i] } return "" } // Channel returns the first param without '#', if it is a channel. func (m Message) Channel() string { if len(m.Params) > 0 && strings.HasPrefix(m.Params[0], "#") { return m.Params[0][1:] } return "" } // Trailing returns the last param (the chat text for PRIVMSG). func (m Message) Trailing() string { if len(m.Params) == 0 { return "" } return m.Params[len(m.Params)-1] } // Parse turns one line into a Message. // Format: [@tags ][:prefix ]COMMAND [params] [:trailing] func Parse(line string) Message { m := Message{Raw: line, Tags: map[string]string{}} rest := line if strings.HasPrefix(rest, "@") { end := strings.IndexByte(rest, ' ') if end < 0 { return m } for _, kv := range strings.Split(rest[1:end], ";") { k, v, _ := strings.Cut(kv, "=") m.Tags[k] = unescapeTag(v) } rest = rest[end+1:] } if strings.HasPrefix(rest, ":") { end := strings.IndexByte(rest, ' ') if end < 0 { return m } m.Prefix = rest[1:end] rest = rest[end+1:] } for rest != "" { if strings.HasPrefix(rest, ":") { m.Params = append(m.Params, rest[1:]) break } word, after, found := strings.Cut(rest, " ") if m.Command == "" { m.Command = word } else { m.Params = append(m.Params, word) } if !found { break } rest = after } return m } // IRCv3 tag values escape ';' ' ' '\' CR and LF. func unescapeTag(v string) string { if !strings.Contains(v, `\`) { return v } var b strings.Builder for i := 0; i < len(v); i++ { if v[i] != '\\' || i == len(v)-1 { if v[i] != '\\' { b.WriteByte(v[i]) } continue } i++ switch v[i] { case ':': b.WriteByte(';') case 's': b.WriteByte(' ') case 'r': b.WriteByte('\r') case 'n': b.WriteByte('\n') default: // "\\" and unknown escapes b.WriteByte(v[i]) } } return b.String() } Step 2: The connection
The connection has more rules than the parser:
- Login. Request the
twitch.tv/tagsandtwitch.tv/commandscapabilities, thenPASSandNICK. For read-only access, anyjustinfanplus digits nick works with any password. With an OAuth token you'd sendPASS oauth:<token>and your own login instead. - One frame, several lines. Twitch's docs warn that a single WebSocket message can hold several IRC lines separated by
\r\n. Split every frame, or you'll parse a batch as one message and drop the rest. - PING/PONG. Twitch sends
PING :tmi.twitch.tvabout every five minutes and drops you if you don't answer. In my run they arrived at 11:28:37 and 11:33:36. - RECONNECT. Twitch sends this before restarting a server. Reconnect right away.
- Join limits. Twitch's docs say 20 join attempts per 10 seconds for a normal account, and 100 channels joined at the same time. The code waits 11 seconds after every 15 joins.
- Silent failures. Joining a channel that doesn't exist returns nothing at all: no error, no
NOTICE. I joinedthisuserdoesnotexist_zz9along with the two real channels and only gotJOINconfirmations for the real ones. Check for theJOINecho.
conn.go:
package main import ( "context" "errors" "fmt" "log" "math/rand/v2" "strings" "time" "github.com/coder/websocket" ) var ircURL = "wss://irc-ws.chat.twitch.tv:443" // var so tests can point it elsewhere // errReconnect means Twitch asked us to move to another server. var errReconnect = errors.New("server sent RECONNECT") // session runs one WebSocket connection until it drops or ctx ends. // Every parsed line goes to out. func session(ctx context.Context, channels []string, out chan<- Message) error { dialCtx, cancel := context.WithTimeout(ctx, 15*time.Second) defer cancel() c, _, err := websocket.Dial(dialCtx, ircURL, nil) if err != nil { return fmt.Errorf("dial: %w", err) } defer c.CloseNow() c.SetReadLimit(1 << 20) // a burst of long messages can exceed the 32 KB default send := func(line string) error { wctx, cancel := context.WithTimeout(ctx, 10*time.Second) defer cancel() return c.Write(wctx, websocket.MessageText, []byte(line+"\r\n")) } // Anonymous, read-only login: any justinfan<digits> nick, any PASS. nick := fmt.Sprintf("justinfan%d", 10000+rand.IntN(80000)) for _, l := range []string{ "CAP REQ :twitch.tv/tags twitch.tv/commands", "PASS SCHMOOPIIE", "NICK " + nick, } { if err := send(l); err != nil { return err } } // JOINs are limited (20 per 10 s for normal users), so pace them. go func() { for i, ch := range channels { if i > 0 && i%15 == 0 { select { case <-time.After(11 * time.Second): case <-ctx.Done(): return } } if err := send("JOIN #" + strings.ToLower(ch)); err != nil { return } } }() for { // Twitch PINGs about every 5 minutes; 6 minutes of silence = dead link. rctx, cancel := context.WithTimeout(ctx, 6*time.Minute) _, data, err := c.Read(rctx) cancel() if err != nil { return fmt.Errorf("read: %w", err) } // One WebSocket frame can carry several IRC lines. for _, line := range strings.Split(strings.TrimRight(string(data), "\r\n"), "\r\n") { if line == "" { continue } m := Parse(line) switch m.Command { case "PING": if err := send("PONG :" + m.Trailing()); err != nil { return err } case "RECONNECT": return errReconnect case "NOTICE": log.Printf("NOTICE %s: %s (msg-id=%s)", m.Channel(), m.Trailing(), m.Tags["msg-id"]) } select { case out <- m: case <-ctx.Done(): return ctx.Err() } } } } // run keeps a session alive with exponential backoff until ctx ends. func run(ctx context.Context, channels []string, out chan<- Message) { backoff := time.Second for ctx.Err() == nil { start := time.Now() err := session(ctx, channels, out) if ctx.Err() != nil { return } if time.Since(start) > time.Minute || errors.Is(err, errReconnect) { backoff = time.Second // it was healthy; reconnect quickly } log.Printf("connection ended: %v; reconnecting in %s", err, backoff) select { case <-time.After(backoff): case <-ctx.Done(): return } backoff = min(backoff*2, 2*time.Minute) } } ircURL is a var so you can point a test build somewhere unreachable and watch the backoff work. I built one with -ldflags "-X main.ircURL=wss://127.0.0.1:9":
2026/11/20 11:23:37 connection ended: dial: failed to WebSocket dial: failed to send handshake request: Get "https://127.0.0.1:9": dial tcp 127.0.0.1:9: connect: connection refused; reconnecting in 1s 2026/11/20 11:23:38 connection ended: dial: failed to WebSocket dial: failed to send handshake request: Get "https://127.0.0.1:9": dial tcp 127.0.0.1:9: connect: connection refused; reconnecting in 2s 2026/11/20 11:23:40 connection ended: dial: failed to WebSocket dial: failed to send handshake request: Get "https://127.0.0.1:9": dial tcp 127.0.0.1:9: connect: connection refused; reconnecting in 4s 2026/11/20 11:23:44 connection ended: dial: failed to WebSocket dial: failed to send handshake request: Get "https://127.0.0.1:9": dial tcp 127.0.0.1:9: connect: connection refused; reconnecting in 8s 2026/11/20 11:23:52 connection ended: dial: failed to WebSocket dial: failed to send handshake request: Get "https://127.0.0.1:9": dial tcp 127.0.0.1:9: connect: connection refused; reconnecting in 16s 2026/11/20 11:23:57 done: 0 lines, 0 chat messages stored The backoff resets after a connection that lasted more than a minute, so a normal RECONNECT doesn't leave you waiting two minutes.
Step 3: Store messages, and honor deletions
This is where most chat loggers are sloppy. Three things deserve care.
Moderation. CLEARMSG means a moderator deleted one message (target-msg-id). CLEARCHAT with a target-user-id is a timeout (when ban-duration is present) or a ban, and Twitch hides that user's recent messages. CLEARCHAT with no target clears the whole chat. The code keeps the row (time, user, channel) but empties the text and records why. In 22 minutes of logging I saw 74 CLEARCHAT events and no CLEARMSG at all. Timeouts are how these chats get moderated: 375 of 7,360 #xqc messages (5.1%) ended up redacted.
Emote positions are code points, not bytes. The emotes tag says 425618:2-4 and means characters 2 to 4. With an emoji earlier in the message, slicing the UTF-8 bytes gives you garbage. Slicing a []rune is correct. I checked all 1,702 emote ranges from my first two runs: 3 of them would have come out wrong with byte slicing, all in one message that started with ✌🏾. That's rare, and it's exactly the kind of bug that slips through testing.
Invisible characters. Twitch rejects the same message sent twice in a row, so chat clients append an invisible character to get around it. In my first two runs, 219 of 11,662 messages ended in U+034F (combining grapheme joiner) and 22 in U+E0000. Strip them before storing, or "KEKW KEKW" and "KEKW KEKW\u034f" count as different messages and searches miss.
Twitch's docs also say the server "may also send a message multiple times" if it thinks you didn't get it, so the message id is the primary key and inserts use INSERT OR IGNORE.
store.go:
package main import ( "database/sql" "strconv" "strings" "time" _ "modernc.org/sqlite" ) const schema = ` PRAGMA journal_mode=WAL; CREATE TABLE IF NOT EXISTS messages ( id TEXT PRIMARY KEY, channel TEXT, room_id TEXT, user_id TEXT, login TEXT, display_name TEXT, text TEXT, sent_at INTEGER, badges TEXT, sub_months INTEGER, bits INTEGER, first_msg INTEGER, reply_to TEXT, emotes TEXT, deleted TEXT); CREATE INDEX IF NOT EXISTS messages_chan_time ON messages(channel, sent_at); CREATE INDEX IF NOT EXISTS messages_user ON messages(user_id); CREATE VIRTUAL TABLE IF NOT EXISTS messages_fts USING fts5( text, content='messages', content_rowid='rowid'); CREATE TRIGGER IF NOT EXISTS messages_ai AFTER INSERT ON messages BEGIN INSERT INTO messages_fts(rowid, text) VALUES (new.rowid, new.text); END; CREATE TRIGGER IF NOT EXISTS messages_au AFTER UPDATE OF text ON messages BEGIN INSERT INTO messages_fts(messages_fts, rowid, text) VALUES ('delete', old.rowid, old.text); INSERT INTO messages_fts(rowid, text) VALUES (new.rowid, new.text); END; CREATE TABLE IF NOT EXISTS events ( id TEXT PRIMARY KEY, channel TEXT, kind TEXT, login TEXT, system_msg TEXT, text TEXT, sent_at INTEGER, months INTEGER); CREATE TABLE IF NOT EXISTS roomstate ( channel TEXT PRIMARY KEY, room_id TEXT, slow INTEGER, followers_only INTEGER, emote_only INTEGER, subs_only INTEGER, updated_at INTEGER);` func openDB(path string) (*sql.DB, error) { db, err := sql.Open("sqlite", path) if err != nil { return nil, err } db.SetMaxOpenConns(1) if _, err := db.Exec(schema); err != nil { return nil, err } return db, nil } func atoi(s string) int { n, _ := strconv.Atoi(s); return n } func sentAt(m Message) int64 { if ts, err := strconv.ParseInt(m.Tags["tmi-sent-ts"], 10, 64); err == nil { return ts } return time.Now().UnixMilli() } // subMonths reads "subscriber/14" out of badge-info. func subMonths(badgeInfo string) int { for _, b := range strings.Split(badgeInfo, ",") { if k, v, ok := strings.Cut(b, "/"); ok && (k == "subscriber" || k == "founder") { return atoi(v) } } return 0 } // emoteNames maps the emotes tag ("25:0-4,6-10/1902:12-16") to names. // Positions count Unicode code points, not bytes, so index into []rune. func emoteNames(tag, text string) []string { if tag == "" { return nil } runes := []rune(text) var names []string for _, e := range strings.Split(tag, "/") { _, ranges, _ := strings.CutSource:https://dev.to/viktorhendelmann/how-to-scrape-twitch-with-go-chat-logs-over-irc-websockets-moderation-events-full-text-search-54c7Related posts
Replaying Yesterday: Designing a Date Picker for a Daily Puzzle
A refusal that changes no bytes
NestJS for Spring Boot developers: a TypeScript backend that will feel familiar
NestJS for ASP.NET Core developers: a TypeScript backend that will feel familiar
