Telegram Web K offloads almost every expensive operation away from the browser’s main thread. Four distinct worker contexts handle networking, caching, cryptography, and animation — leaving the main thread free to respond to user input and update the UI without stalling. This document explains what each worker does, where its code lives, and how they all talk to each other.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/TelegramOrg/Telegram-web-k/llms.txt
Use this file to discover all available pages before exploring further.
Shared Worker
Entry point:src/lib/mainWorker/index.worker.ts
The Shared Worker is the backbone of the application. Because it is a SharedWorker (not a plain Worker), a single instance is shared across every browser tab that has the app open. This means state is consistent across tabs — opening a new tab does not create a second copy of the networking stack.
What it does
- Hosts all 55+ domain managers (
appMessagesManager,appChatsManager,appUsersManager, etc.) fromsrc/lib/appManagers/. - Runs the full MTProto implementation: session management, encryption, data centre selection, and transport negotiation (
src/lib/mtproto/). - Maintains persistent application state via IndexedDB (
src/lib/storages/) and localStorage (src/lib/localStorage.ts). - Relays
rootScopebroadcast events back to every connected main-thread tab throughsuperMessagePort. - Manages the auto-lock feature (
src/lib/mainWorker/useAutoLock.ts) for passcode-protected accounts.
How the main thread connects
Whensrc/index.ts starts, it calls getProxiedManagers() (src/lib/getProxiedManagers.ts), which creates a JavaScript Proxy over the apiManagerProxy. Every manager method call is transparently serialised into a WorkerTask and sent over superMessagePort to the Shared Worker. The result is returned as a Promise.
Fallback: noSharedWorker mode
IfSharedWorker is not supported in the current browser, or if you pass the noSharedWorker=1 query parameter, the worker logic runs inside a regular Worker instead. This is also useful during debugging to get a unified DevTools context.
Service Worker
Entry point:sw.ts → src/lib/serviceWorker/index.service.ts
The Service Worker intercepts network requests and handles push notifications, file streaming, and background tasks. It runs independently of any open tab and can receive push events even when the app is closed.
Responsibilities
| Module | File | What it handles |
|---|---|---|
| Cache | src/lib/serviceWorker/cache.ts | CacheStorage API — assets, API responses |
| Push notifications | src/lib/serviceWorker/push.ts | Web Push, notification display, click handling |
| File streaming | src/lib/serviceWorker/stream.ts | Range-request streaming for media files |
| File downloads | src/lib/serviceWorker/download.ts | Triggered download responses |
| Share target | src/lib/serviceWorker/share.ts | Web Share Target API handling |
| RTMP streams | src/lib/serviceWorker/rtmp.ts | Live stream proxying |
| HLS | src/lib/hls/ | Adaptive bitrate video playlists and segments |
| Backgrounds | src/lib/serviceWorker/backgrounds.ts | Chat background image caching |
| Cache cleanup | src/lib/serviceWorker/clearOldCache.ts | Pruning stale cache entries |
Communication
The Service Worker communicates with open tabs viaServiceMessagePort (src/lib/serviceWorker/serviceMessagePort.ts), which extends superMessagePort. It also has a direct MessageChannel connection to the Shared Worker so that push events can be routed to the right account without going through the main thread.
Crypto Worker
Entry point:src/lib/crypto/crypto.worker.ts
All cryptographic operations run in a dedicated Worker so that expensive computations — particularly RSA encryption and DH key generation — do not block the Shared Worker’s event loop during session establishment.
Operations handled
How it is invoked
The Shared Worker and Service Worker both communicate with the Crypto Worker viacryptoMessagePort (src/lib/crypto/cryptoMessagePort.ts). The main thread also proxies crypto calls through the Shared Worker when needed:
Lottie Workers
Entry point:src/lib/rlottie/rlottie.worker.ts
Animated stickers in Telegram use the Lottie format. Telegram Web K renders them with rlottie, a C++ Lottie renderer compiled to WebAssembly (src/lib/rlottie/rlottie-wasm.wasm). Because WebAssembly execution and frame decoding are CPU-intensive, they run in one or more dedicated workers.
How it works
lottieLoader.ts(src/lib/rlottie/lottieLoader.ts) manages the pool of Lottie workers.- Each
rlottiePlayer.tsinstance sends animation JSON and frame requests to a worker viarlottieMessagePort.ts. - The worker decodes frames using the WASM module and transfers the raw pixel data back to the main thread as
ArrayBuffertransferables (zero-copy). - Colour replacements for themed stickers are applied by
applyReplacements.tsbefore the frame is drawn to a canvas.
Transferable objects are used for frame pixel data, so decoded frames are moved (not copied) from the worker back to the main thread. This avoids doubling memory usage for large sticker canvases.
Inter-worker communication
All workers usesuperMessagePort (src/lib/superMessagePort.ts) as their message-passing protocol. Its key features:
- Message batching — tasks queued in the same microtask tick are combined into a single
BatchTask, reducingpostMessagecall overhead. - Request/response correlation — each
invoketask gets a unique ID; the correspondingresulttask is matched and resolves the caller’sPromise. - Ack support — a caller can request an
ackmessage before the full result, enabling the receiver to signal a cache hit synchronously. - Web Locks integration — the port sends a
locktask when a tab acquires anavigator.lockslock; the Shared Worker mirrors the lock and is notified when the tab closes, even ifbeforeunloaddoes not fire.
broadcastChannelWrapper (src/lib/broadcastChannelWrapper.ts) wraps the native BroadcastChannel API with typed emit / on methods:
Worker summary
| Worker | Type | Source | Shared across tabs |
|---|---|---|---|
| Main Worker | SharedWorker | src/lib/mainWorker/index.worker.ts | Yes |
| Service Worker | ServiceWorker | sw.ts | Yes (browser-managed) |
| Crypto Worker | Worker | src/lib/crypto/crypto.worker.ts | No (per-SharedWorker) |
| Lottie Workers | Worker (pool) | src/lib/rlottie/rlottie.worker.ts | No (per-tab) |