1. Install LiveKit SDK
Install the LiveKit SDK:
yarn add @livekit/components-react @livekit/components-styles livekit-client
2. Join a room
Update the serverUrl
and token
values and copy and paste the following into your src/App.tsx
file. To generate a token for this example, see Creating a token.
Note
This example hardcodes a token. In a real app, your server generates a token for you.
import {ControlBar,GridLayout,ParticipantTile,RoomAudioRenderer,useTracks,RoomContext,} from '@livekit/components-react';import { Room, Track } from 'livekit-client';import '@livekit/components-styles';import { useState } from 'react';const serverUrl = '<your LiveKit server URL>';const token = '<generate a token>';export default function App() {const [room] = useState(() => new Room({// Optimize video quality for each participant's screenadaptiveStream: true,// Enable automatic audio/video quality optimizationdynacast: true,}));// Connect to roomuseEffect(() => {let mounted = true;const connect = async () => {if (mounted) {await room.connect(serverUrl, token);}};connect();return () => {mounted = false;room.disconnect();};}, [room]);return (<RoomContext.Provider value={room}><div data-lk-theme="default" style={{ height: '100vh' }}>{/* Your custom component with basic video conferencing functionality. */}<MyVideoConference />{/* The RoomAudioRenderer takes care of room-wide audio for you. */}<RoomAudioRenderer />{/* Controls for the user to start/stop audio, video, and screen share tracks */}<ControlBar /></div></RoomContext.Provider>);}function MyVideoConference() {// `useTracks` returns all camera and screen share tracks. If a user// joins without a published camera track, a placeholder track is returned.const tracks = useTracks([{ source: Track.Source.Camera, withPlaceholder: true },{ source: Track.Source.ScreenShare, withPlaceholder: false },],{ onlySubscribed: false },);return (<GridLayout tracks={tracks} style={{ height: 'calc(100vh - var(--lk-control-bar-height))' }}>{/* The GridLayout accepts zero or one child. The child is usedas a template to render all passed in tracks. */}<ParticipantTile /></GridLayout>);}
Next steps
- Set up a server to generate tokens for your app at runtime by following this guide: Generating Tokens.
- If you're looking to dive deeper into building your LiveKit app with React, check out the React Components reference section. There you'll find a comprehensive list of available components and React hooks, along with examples of how to use them. This is a great resource for building more complex and advanced apps.
Happy coding!