XXII Chat: Inside a Local-First Messenger With Calls and a Streaming Studio in Vanilla JS—and Why Its 57K Lines of Vibe Code Need TamingArticle · digest for November 27
Architectural cutaway of the modular browser-based XXII Chat system showing OPFS, WebRTC, and WHIP layers
Article4 sections≈ 8 min

XXII Chat: Inside a Local-First Messenger With Calls and a Streaming Studio in Vanilla JS—and Why Its 57K Lines of Vibe Code Need Taming

Timur Shemsedinov, creator of the Metarhia ecosystem, open-sourced XXII Chat—a browser messenger featuring video calls, local recording, and a live streaming studio. The whole thing is written in pure JavaScript with zero UI frameworks and zero bundlers. Right away, the author admitted candidly: the stack is snappy, but the architecture turned into a 57,000-line 'vibe-coded monster'.

The story hits from two angles. First, it shows how far browser APIs have come when chats, an offline database, video calls, and a stream mixer all live in one window without React or Webpack. Second, it offers a clear lesson on what happens when features are stacked rapidly with AI agents.

Vanilla JS, Zero Bundlers, and 57,000 Lines of Code

Open package.json and it's hard to believe your eyes. There is no React, no Vue, no Vite, and not a single mainstream frontend package. Instead, it relies on five libraries from the Metarhia ecosystem: the impress app server, metacom transport and RPC, globalstorage replica engine, metaice built-in server, and metautil helpers.

The entire frontend 'build' in npm run static consists of three cp commands copying .mjs files from node_modules into application/static. The UI renders using raw DOM APIs and the HTML <template> tag. Not even Web Components or Shadow DOM were used.

Running the app on Node.js 24+ takes three commands:

# Install Metarhia dependencies directly from GitHub branches
npm ci
# Copy client .mjs modules into the static folder
npm run static
# Start the server: API on port 8000, STUN/TURN on 3478
node server

The server runs in a single process with no load balancer so call state stays in one thread. The api group listens on port 8000 (XXII_PORT), while rtc on port 3478 (UDP/TCP, relay range 49160–49359) spins up STUN/TURN via metaice.

If STUN and TURN feel rusty, the idea is simple. When two browsers call each other directly, routers and firewalls get in the way. STUN discovers external addresses to punch a direct connection. If the network blocks direct traffic, TURN steps in and relays media through the server.

On that lean dependency diet, the app packs a surprising punch: chats, post feeds with tables of contents, folders, threads, offline mode, calls, local recording, and live streaming. Over 71 commits, the repo grew to 57,000 lines including tests (24,000 without). It includes over 70 browser test files running headless Chrome with a virtual camera and microphone.

Three CRDT Layers, Browser Storage, and Server-Bypassing DMs

XXII Chat's core technical hook is a local-first design: the messenger works offline and reconciles edits automatically. It uses CRDTs (Conflict-free Replicated Data Types) from globalstorage. Simply put, these are data structures you can edit on separate devices offline that merge back into one consistent state with no manual conflict resolution.

Each record field (message text, reaction, folder, or author name) acts as a last-write-wins register. Deleting a message doesn't erase it physically; it clears the text and sets a deleted flag so reply threads don't break. Both the browser and server validate every change using the exact same application/static/chat.js.

Data flows through three tiers:

  [Server: application/data/chat]
                ^
                | one WebSocket across all tabs (MetacomProxy)
                v
  [Service Worker: replicas in OPFS]
                ^
                | in-browser MessagePort
                v
  [Tab: in-memory replicas] <=== WebRTC mesh ===> [Other browser]
   (one tab holds Web Lock xxii:mesh)

In the browser, data lives in OPFS (Origin Private File System), a hidden local filesystem giving Web Workers fast byte-level disk access. A Service Worker keeps one WebSocket connection shared across all tabs, caches the app, and handles updates: fresh static files download into a new cache, and the tab reloads only when minimized or idle for a minute with no open menus or unsent files. Drafts are preserved.

What if you have five chat tabs open? Spinning up five identical peer meshes would choke your laptop. That's where the Web Locks API helps by coordinating locks across tabs. Exactly one tab acquires the xxii:mesh lock and maintains the WebRTC mesh with online peers.

Files and privacy are handled carefully. Attachments up to 10 MB stay out of CRDT replicas to avoid bloating sync: only metadata and hashes replicate, while raw bytes sit in files/<sha256>. Direct messages and their files never touch the server—they travel peer-to-peer over WebRTC and live only in each user's OPFS. Even if the server goes down, group chats keep syncing over the mesh.

Mesh Calls, Crash-Safe Recording, and WHIP Streaming

The same vanilla UI houses an adaptive-bitrate calling engine. Hidden participant tiles pause camera encoding. Five seconds of poor connection drop quality one step, while twenty clean seconds restore it. If two peers can't connect directly, a third peer temporarily acts as a relay to forward their media.

Studio still life of the XXII Chat broadcast desk with Studio controls and WHIP live indicators

Call recording happens entirely in the browser. The recorder mixes a 'program' feed (grid, active speaker, or screen share with a separate audio mix) at 720p30, 1080p30, or 1080p60 in MP4 (H.264) or WebM (VP9/VP8). A dedicated worker writes one-second chunks to OPFS, flushes buffers every 5 seconds, and saves metadata every 10 seconds.

If a tab crashes, you lose only the last few seconds. On the next launch, a repair script recovers the file and writes the duration into the WebM header. The recorder warns when free disk space drops below 2 GB and stops at 500 MB. Even in a background tab, it keeps rendering video and requests frame sizes matching each tile. In Chromium with the File System Access API, recordings can write straight to a local folder in 5–30 minute segments.

App TaskBrowser API / StandardWhy It Was Chosen
Storing replicas, files & videoOPFSFast byte-level worker disk access without localStorage limits
Single WebRTC mesh per browserWeb Locks APIEnsures exactly one tab manages the peer network
DMs & offline group syncWebRTC DataChannelDirect browser-to-browser transfer bypassing the server
Live stream outputWHIP (RFC 9725)Push WebRTC streams to media servers via one HTTP request

On top of the recorder sits Studio mode: a host moves guests between backstage and stage, switches scenes with 300 ms crossfades, runs lower thirds, tickers, and local clips, and overlays chat messages on screen.

The output can stream live over WHIP (RFC 9725), an IETF standard from March 2025 replacing legacy RTMP. The browser sends the stream to any compatible server like MediaMTX via an HTTP POST with session parameters and a Bearer token (1–20 Mbps, up to two destinations at once). If the media server lacks CORS, initial signaling routes through the stream/whip server relay (rate-limited to 30 requests/minute), while video still flows directly.

Where the Experiment Hits Real Boundaries

package.json is at version 0.0.0, dependencies pull straight from GitHub branches, and the project is an open sandbox rather than an enterprise product. The opt-out recording button relies on client honesty: a modified browser could ignore it. Writing directly to disk folders works only in Chromium, and crash recovery requires synchronous OPFS access in a worker plus Web Locks.

Those limits are fair for early open source. What's striking is that a Slack, Zoom, and OBS combo runs from one repository without a single bundler.

Why AGENTS.md and 70 Tests Didn't Save the Architecture From Vibe Code

Seeing XXII Chat's feature list, it's tempting to think one person with an AI can ship complex systems in weeks. The author himself cools that hype: the code works and passes tests, yet inside it's 'wild, unreadable vibe code' with terrible architecture.

On paper, discipline looked tight. The repo root has an AGENTS.md with seven rules for AI agents: no extra dependencies, 80-character line limits, types in .d.ts without JSDoc, code split into domain/, schemas/, and static/, minimal comments, and mandatory ESLint plus Prettier.

Here is the price of speed: linters and dozens of tests catch small bugs, but they don't stop architectural sprawl. An AI agent looks at nearby files, neatly bolts on another feature, and gets green tests—while module coupling quietly turns into tangled spaghetti. By commit 71, the project ballooned to 24,000 lines of app code and 33,000 lines of tests, too big for a human to hold in their head.

Shemsedinov doesn't plan to rewrite it in a framework. Instead, he wants top-down order: mapping the repository and building an architectural DSL that formally enforces subsystem boundaries so agents can't mix everything together. Whether that rein works on a live 57,000-line codebase will be worth watching.

Want a deep diveVotes help us choose what to break down in detail. This material has 0 votes.
Sources · 4