JavaScript SDK

Connect to RelayKit rooms from any JavaScript application using the LiveKit client SDK. Works with vanilla JS, Vue, Svelte, Angular, or any framework.

Installation

npm install livekit-client

Quick Start

import { Room, RoomEvent, Track } from 'livekit-client';

// 1. Get a token from your server (which calls RelayKit API)
const res = await fetch('/api/get-token', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    roomId: 'your-room-id',
    userId: 'user_123',
    userName: 'Alice',
  }),
});
const { token } = await res.json();

// 2. Connect to the room
const room = new Room();

room.on(RoomEvent.TrackSubscribed, (track, publication, participant) => {
  if (track.kind === Track.Kind.Video) {
    const element = track.attach();
    document.getElementById('video-grid').appendChild(element);
  } else if (track.kind === Track.Kind.Audio) {
    const element = track.attach();
    document.body.appendChild(element);
  }
});

room.on(RoomEvent.TrackUnsubscribed, (track) => {
  track.detach().forEach((el) => el.remove());
});

room.on(RoomEvent.ParticipantConnected, (participant) => {
  console.log(participant.identity, 'joined');
});

room.on(RoomEvent.Disconnected, () => {
  console.log('Disconnected from room');
});

await room.connect('wss://livekit.relaykit.live', token);
console.log('Connected to:', room.name);

// 3. Publish camera and microphone
await room.localParticipant.enableCameraAndMicrophone();

Server-side: Generate tokens

Important

Never expose your API key (rk_live_xxxx) in client-side code. Always generate tokens on your server.

// Your server (Node.js, Python, Go, etc.)
// Step 1: Create a room
const roomRes = await fetch('https://api.relaykit.live/api/v1/rooms', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer rk_live_YOUR_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ displayName: 'Team Meeting' }),
});
const { data: room } = await roomRes.json();
// room.id = "uuid", room.meetingCode = "abc-defg-hij"

// Step 2: Generate a token for a participant
const tokenRes = await fetch('https://api.relaykit.live/api/v1/token', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer rk_live_YOUR_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    roomId: room.id,
    participantIdentity: 'user_123',
    participantName: 'Alice',
  }),
});
const { data: tokenData } = await tokenRes.json();
// tokenData.token = "eyJ..." (send this to the client)
// tokenData.roomName = "abc-defg-hij"

Room Controls

// Toggle camera
await room.localParticipant.setCameraEnabled(false);
await room.localParticipant.setCameraEnabled(true);

// Toggle microphone
await room.localParticipant.setMicrophoneEnabled(false);

// Share screen
await room.localParticipant.setScreenShareEnabled(true);

// Disconnect
await room.disconnect();

// Get all participants
room.remoteParticipants.forEach((p) => {
  console.log(p.identity, p.name);
});

Events Reference

RoomEvent.TrackSubscribed

A remote participant published a track (video or audio)

RoomEvent.TrackUnsubscribed

A remote track was removed

RoomEvent.ParticipantConnected

A new participant joined the room

RoomEvent.ParticipantDisconnected

A participant left the room

RoomEvent.Disconnected

You were disconnected from the room

RoomEvent.ActiveSpeakersChanged

List of currently speaking participants updated

Full Reference

See the LiveKit JavaScript SDK documentation for the complete API reference.