wonderful-train-57
02/24/2025, 12:20 PMgray-monkey-83525
02/24/2025, 12:20 PMyarn add livekit-client @livekit/components-react @livekit/components-styles
2. Create a Token Endpoint: You need a server-side endpoint to generate access tokens for participants. This is typically done using the livekit-server-sdk
. Here's an example of how to create a token endpoint in Next.js:
import { NextApiRequest, NextApiResponse } from 'next';
import { AccessToken } from 'livekit-server-sdk';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const room = req.query.room as string;
const username = req.query.username as string;
if (!room || !username) {
return res.status(400).json({ error: 'Missing room or username' });
}
const apiKey = process.env.LIVEKIT_API_KEY;
const apiSecret = process.env.LIVEKIT_API_SECRET;
const token = new AccessToken(apiKey, apiSecret, { identity: username });
token.addGrant({ room, roomJoin: true, canPublish: true, canSubscribe: true });
res.status(200).json({ token: await token.toJwt() });
}
3. Create a Video Conference Page: In your Next.js app, create a page to handle the video conference. Use the LiveKitRoom
component to manage the room and participants:
import { LiveKitRoom, VideoConference } from '@livekit/components-react';
import '@livekit/components-styles';
import { useEffect, useState } from 'react';
export default function VideoConferencePage() {
const [token, setToken] = useState('');
useEffect(() => {
const fetchToken = async () => {
const response = await fetch(`/api/token?room=myRoom&username=myUser`);
const data = await response.json();
setToken(data.token);
};
fetchToken();
}, []);
if (!token) {
return <div>Loading...</div>;
}
return (
<LiveKitRoom
video={true}
audio={true}
token={token}
serverUrl={process.env.NEXT_PUBLIC_LIVEKIT_URL}
data-lk-theme="default"
style={{ height: '100vh' }}
>
<VideoConference />
</LiveKitRoom>
);
}
This setup will allow you to manage both local and remote participants in a LiveKit room. The LiveKitRoom
component handles the connection to the room, and the VideoConference
component provides a ready-made UI for video conferencing.
Would you like more detailed guidance on any specific part of this setup?
Sources:
- Next.js 13 Quickstart 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.