LiveKit Client SDK

2.18.3 · active · verified Sun Apr 19

The `livekit-client` package provides a JavaScript/TypeScript SDK for building real-time communication applications featuring video, audio, and data. It connects to LiveKit Cloud or self-hosted LiveKit servers, enabling developers to integrate functionalities like multi-modal AI, live streaming, and video calls. The current stable version is 2.18.3, with a release cadence featuring frequent patch updates and minor versions typically every one to two weeks. Key differentiators include its focus on performance optimizations like adaptive streaming and dynacast, robust event-driven architecture, and comprehensive support for various media types and advanced features like End-to-End Encryption (E2EE) and data tracks, offering a powerful platform for interactive media applications.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a LiveKit room, handle remote participant track subscriptions, publish local camera and microphone, and manage room events, suitable for browser-based applications.

import {
  LocalParticipant,
  LocalTrackPublication,
  Participant,
  RemoteParticipant,
  RemoteTrack,
  RemoteTrackPublication,
  Room,
  RoomEvent,
  Track,
  VideoPresets,
} from 'livekit-client';

async function setupLiveKitRoom() {
  const url = process.env.LIVEKIT_URL ?? 'ws://localhost:7800'; // Replace with your LiveKit server URL
  const token = process.env.LIVEKIT_TOKEN ?? 'YOUR_LIVEKIT_TOKEN'; // Replace with your LiveKit access token

  if (token === 'YOUR_LIVEKIT_TOKEN') {
    console.error('Please provide a LiveKit access token. You can generate one from your LiveKit server or dashboard.');
    return;
  }

  const room = new Room({
    adaptiveStream: true,
    dynacast: true,
    videoCaptureDefaults: {
      resolution: VideoPresets.h720.resolution,
    },
  });

  // Ensure a parent element exists to attach video/audio streams
  const parentElement = document.getElementById('livekit-container') || document.body;

  room.on(RoomEvent.TrackSubscribed, (track: RemoteTrack, publication: RemoteTrackPublication, participant: RemoteParticipant) => {
    if (track.kind === Track.Kind.Video || track.kind === Track.Kind.Audio) {
      const element = track.attach();
      parentElement.appendChild(element);
      console.log(`Track subscribed: ${track.kind} from ${participant.identity}`);
    }
  });

  room.on(RoomEvent.TrackUnsubscribed, (track: RemoteTrack, publication: RemoteTrackPublication, participant: RemoteParticipant) => {
    const element = track.detach();
    element.remove();
    console.log(`Track unsubscribed: ${track.kind} from ${participant.identity}`);
  });

  room.on(RoomEvent.ActiveSpeakersChanged, (speakers: Participant[]) => {
    console.log('Active speakers:', speakers.map(s => s.identity));
  });

  room.on(RoomEvent.Disconnected, (reason: string | undefined) => {
    console.log('Disconnected from room:', reason);
  });

  room.on(RoomEvent.LocalTrackUnpublished, (publication: LocalTrackPublication, participant: LocalParticipant) => {
    console.log('Local track unpublished:', publication.trackSid);
  });

  console.log('Preparing connection...');
  room.prepareConnection(url, token);

  try {
    await room.connect(url, token);
    console.log('Connected to room:', room.name);
    await room.localParticipant.enableCameraAndMicrophone();
    console.log('Published local camera and microphone.');
  } catch (error) {
    console.error('Failed to connect to LiveKit room:', error);
  }
}

// Call the setup function when the DOM is ready in a browser environment
if (typeof document !== 'undefined') {
  document.addEventListener('DOMContentLoaded', setupLiveKitRoom);
} else {
  // Fallback for non-browser environments or immediate execution
  setupLiveKitRoom();
}

view raw JSON →