Skip to content

feat: YouTube live chat in the Apple TV player - #2

Merged
Priveetee merged 8 commits into
mainfrom
feat/youtube-live-chat
Nov 6, 2026
Merged

Priveetee merged 8 commits into
mainfrom
feat/youtube-live-chat

Conversation

@Toastienergy

@Toastienergy Toastienergy commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

What

YouTube live chat in the Apple TV player, like Android TV (LiveChatApi + LiveChatLayer), reading the server's GET /live-chat SSE stream. The model, parser, reconnect service and transport are in the shared code, so an iOS chat can reuse them; only the column is tvOS.

While a YouTube live plays, the picture keeps the left of the screen and the chat gets its own opaque 480 pt column on the right (Android TV: 320 dp, same idea). It is open by default and switched by a Chat chip next to Captions. The column never takes the focus.

Read first

  • Server: YoutubeLiveChatRoutes.kt, openapi/paths/live-chat.yaml. Events message ({id, text, receivedAtMs, authorName?, authorAvatarUrl?, moderator}) and error, : keep-alive every 15 s; refusals 422 / 502 / 429 (Retry-After: 5).
  • Android TV: LiveChatApi, LiveChatSource (backoff 1/2/4/8/15/30 s, six failures, reset by a message, 422 stops, 429 waits for Retry-After), LiveChatLayer (400 ms publishing, newest lines that fit, 30 kept).
  • Web: use-live-chat-connection.ts, api-live-chat.ts (an ended event that the OpenAPI text does not mention; 4xx other than 408/429 stop; 5 retries).

Commits (each under 390 changed lines)

  1. LiveChatModels, LiveSSEParser, LiveChatEventDecoder: byte-wise SSE parser (lines split before UTF-8 is read, so chunk size does not matter), message/ended/error decoding, one clean line of text (U+FFFC dropped, ZWJ kept so emoji families stay whole).
  2. LiveChatService + LiveChatRetry: reconnect rules above; the consumer's cancellation cancels the transport.
  3. URLSessionLiveChatTransport + AppDependencies.liveChatService: a session of its own, because the API and media sessions end a transfer after 120 to 180 s and a live runs for hours (idle timeout 60 s against the 15 s keep-alive).
  4. LiveChatBuffer: 30 newest messages, each id once (a reconnect may replay), connection status.
  5. tvOS: TVLiveChatModel (publishes 4 times a second), TVLiveChatPanel, the picture layer split out of TVPlayerScreen, the chip, English and French strings.

Decisions to check

  • 401 retries like a 5xx instead of stopping (the web client refreshes through authed; here another request may have renewed the token by the time of the reconnect). Bounded by the six-failure budget.
  • No history is cleared on reconnect (web does): messages are de-duplicated by id instead.
  • Text is plain: Android TV runs HtmlText.plain over it; the server sends plain text (commentText), so only invisible characters and whitespace are cleaned.
  • No avatars (Android TV shows none; the server sends authorAvatarUrl).
  • The column hides when the server sends ended, stays with a message when unavailable (Android TV: same).
  • Width 480 pt is my choice for a 1920 pt canvas; adjust if it looks wrong.

Verification

  • The shared sources compile with Swift 6.0.3 on a Linux host and 38 XCTest cases pass (parser at chunk sizes 1 to whole, decoder, 13 service cases with a scripted transport incl. backoff, Retry-After, 4xx, error events, network cut and "stopping the consumer closes the connection", buffer).
  • CI on adbb51d (macOS runner): the iOS workflow passed, with the full unit test suite in the iOS simulator (including the transport tests: request URL and headers, refusal with Retry-After, the service reading a real SSE body through URLProtocolStub), the unsigned iOS build and the unsigned tvOS build. That is the first compile of the tvOS column, the chip and the picture layer.
  • Not verified: nothing ran against a real YouTube live or on an Apple TV / the simulator: the actual look of the column, how the picture resizes, the focus when the chip is pressed, the Retry-After of a real 429.

AI disclosure

Prepared with AI assistance (Claude); built and run by CI only.

Toastienergy and others added 8 commits November 6, 2026 18:31
The shared half of YouTube live chat for the Apple clients, Foundation only. The parser reads the server's /live-chat stream byte by byte (lines split before any UTF-8 is decoded, blank line ends an event, keep-alive comments ignored). The decoder turns message, ended and error events into LiveChatEvent; text becomes one clean line without U+FFFC, which would draw as a boxed OBJ, while the zero-width joiner stays so emoji families stay whole.
Opens the server's /live-chat stream through a transport and turns it into LiveChatEvent values. Reconnect rules: Android TV's backoff (1, 2, 4, 8, 15, 30 s) and budget of six failures, reset by any delivered message; 429 waits for Retry-After (at most 60 s); 401, 408 and 5xx retry; 422 and other 4xx stop at once, as the web client does. An error event or a closed stream reconnects, an ended event finishes. Cancelling the consumer cancels the transport, so the connection closes and the server stops its session.
…e app

The transport has a session of its own: the API and media sessions end a transfer after 120 to 180 seconds, a live runs for hours; the idle timeout is a minute because the server sends a keep-alive every 15 seconds. A public instance needs no token, a signed-in session sends it. A refusal carries its status and Retry-After. AppDependencies gets liveChatService.
What the chat column shows, as a value type the player can test: the newest 30 messages, each id shown once (a reconnect may replay the last batch), and where the connection stands. Only YouTube lives have a chat on the server.
Port of Android TV's LiveChatLayer. While a YouTube live plays the picture keeps the left of the screen and the chat its own opaque 480 pt column on the right, open by default and switched by a Chat chip next to Captions. The column never takes the focus. Messages are published four times a second, the newest lines that fit are shown (a partly visible line at the top is left out), and the connection closes when the column does, so the server stops its session. Unavailable and connecting states have their own text; English and French strings added.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants