Skip to content

Commit

Permalink
chore(app): comment code critical for understanding
Browse files Browse the repository at this point in the history
  • Loading branch information
rektdeckard committed Nov 14, 2024
1 parent fb5880a commit 2a58389
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
35 changes: 20 additions & 15 deletions app/api/connection-details/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from "livekit-server-sdk";
import { NextResponse } from "next/server";

// NOTE: you are expected to define the following environment variables in `.env.local`:
const API_KEY = process.env.LIVEKIT_API_KEY;
const API_SECRET = process.env.LIVEKIT_API_SECRET;
const LIVEKIT_URL = process.env.LIVEKIT_URL;
Expand All @@ -18,33 +19,35 @@ export type ConnectionDetails = {

export async function GET() {
try {
// Generate participant token
const participantIdentity = `voice_assistant_user_${Math.round(
Math.random() * 10_000
)}`;
const participantToken = await createParticipantToken(
{
identity: participantIdentity,
},
"roomName"
);

if (LIVEKIT_URL === undefined) {
throw new Error("LIVEKIT_URL is not defined");
}
if (API_KEY === undefined) {
throw new Error("LIVEKIT_API_KEY is not defined");
}
if (API_SECRET === undefined) {
throw new Error("LIVEKIT_API_SECRET is not defined");
}

// Generate participant token
const participantIdentity = `voice_assistant_user_${Math.floor(Math.random() * 10_000)}`;
const roomName = `voice_assistant_room_${Math.floor(Math.random() * 10_000)}`;
const participantToken = await createParticipantToken(
{ identity: participantIdentity },
roomName,
);

// Return connection details
const data: ConnectionDetails = {
serverUrl: LIVEKIT_URL,
roomName: "voice_assistant_room",
roomName,
participantToken: participantToken,
participantName: participantIdentity,
};
return NextResponse.json(data);
} catch (error) {
if (error instanceof Error) {
console.error(error);

return new NextResponse(error.message, { status: 500 });
}
}
Expand All @@ -54,8 +57,10 @@ function createParticipantToken(
userInfo: AccessTokenOptions,
roomName: string
) {
const at = new AccessToken(API_KEY, API_SECRET, userInfo);
at.ttl = "5m";
const at = new AccessToken(API_KEY, API_SECRET, {
...userInfo,
ttl: "15m",
});
const grant: VideoGrant = {
room: roomName,
roomJoin: true,
Expand Down
11 changes: 10 additions & 1 deletion app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,18 @@ export default function Page() {
const [agentState, setAgentState] = useState<AgentState>("disconnected");

const onConnectButtonClicked = useCallback(async () => {
// Generate room connection details, including:
// - A random Room name
// - A random Participant name
// - An Access Token to permit the participant to join the room
// - The URL of the LiveKit server to connect to
//
// In real-world application, you would likely allow the user to specify their
// own participant name, and possibly to choose from existing rooms to join.

const url = new URL(
process.env.NEXT_PUBLIC_CONN_DETAILS_ENDPOINT ??
"/api/connection-details",
"/api/connection-details",
window.location.origin
);
const response = await fetch(url.toString());
Expand Down

0 comments on commit 2a58389

Please sign in to comment.