How livekit/client-sdk-js works
Project Type and Purpose
• This is a JavaScript/TypeScript client SDK (the “livekit-client” library) that your application code imports to provide realtime video, audio, data and RPC capabilities by connecting to a LiveKit server (hosted or self-managed).
High-Level Architecture from the File Tree & README
• Public API layer (src/index.ts, src/options.ts, src/logger.ts)
• Signaling layer (src/api/SignalClient.ts + utils)
• Connection pre-flight checks (src/connectionHelper/*)
• Core media transport (src/room/RTCEngine.ts, PCTransport.ts, PCTransportManager.ts)
• Room and participant management (src/room/Room.ts, LocalParticipant.ts, RemoteParticipant.ts, events.ts)
• Track management (src/room/track/*: Local/Remote tracks, publications, processors)
• End-to-end encryption (src/e2ee/* + e2ee.worker.ts)
• RPC subsystem (src/room/rpc.ts)
• Utility modules (src/utils, src/test mocks, type-polyfills)
• Build & distribution (rollup.config.js, tsconfig.json, vite.config.mjs, package.json)
• Example apps (examples/demo, examples/rpc)
Main Components
• Client Application (your code)
• Public SDK API
• SignalClient (WebSocket signaling)
• ConnectionHelper with its Checks (network, TURN, reconnect, WebRTC/browser feature detection)
• RTCEngine & PCTransport layers (WebRTC PeerConnection + ICE/DTLS)
• Room Manager → Participant Manager → Track Manager sublayers
• E2EE subsystem (E2eeManager + Worker thread for frame encryption/decryption)
• RPC endpoint (method registration and invocation over data channel)
• LiveKit Server (external service)
Relationships & Data Flows
• App → Public API (Room.prepareConnection / .connect / publishTrack / RPC calls)
• Public API → SignalClient (opens WS to LiveKit server)
• SignalClient ↔ LiveKit Server (SDP offer/answer, ICE candidates, RPC messages)
• Public API → ConnectionHelper (runs ordered checks before/after connect)
• Room → RTCEngine → PCTransport (manages PeerConnection lifecycle)
• Local media (getUserMedia) → TrackManager → PCTransport → network (encrypted via E2EE Worker if enabled)
• Network → PCTransport → RTCEngine → TrackManager → Room emits TrackSubscribed events to App
• App subscribes to RoomEvent, attaches HTMLMediaElement to track streams
• RPC subsystem uses signaling channel to send/receive pre-registered method calls
Architectural Patterns & Design Principles
• Layered separation of concerns: API → Signaling → Transport → Application Events
• Modular checks pipeline (ConnectionHelper) for feature detection and fallbacks
• Worker-based E2EE encryption offloading CPU work from main thread
• Event-driven design: Room and Participants emit domain events
• Type-safe boundaries via TypeScript interfaces/types in each module
Essential Elements Checklist
• LiveKit Server drawn as external system with WebSocket and ICE endpoints