how can I connect my vite+react frontend with agen...
# ask-ai
f
how can I connect my vite+react frontend with agent?
t
Thanks for reaching out, Mridul. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Let's walk through how you can connect your Vite + React frontend with a LiveKit agent step by step: 1. Install Required Dependencies: You'll want to use
@livekit/components-react
and
livekit-client
for smooth integration. You can easily install them with:
npm install @livekit/components-react livekit-client
2. Connect to a LiveKit Room: The
LiveKitRoom
component helps you connect to the room where your agent is active. Here's a simple example:
Copy code
import { LiveKitRoom, VideoConference } from '@livekit/components-react';
import '@livekit/components-styles';

function App() {
  return (
    <LiveKitRoom url="wss://your-livekit-server-url" token="your-participant-token" connect={true}> 
      <VideoConference /> 
    </LiveKitRoom>
  );
}
3. Agent Communication: Your agents will communicate through audio tracks, text streams, or data messages. Make sure your frontend subscribes to these streams to stay connected. 4. Optional Voice Assistant: If you're interested in voice interactions, the
useVoiceAssistant
hook is a great way to integrate a voice-based agent. 5. Proxy Configuration: For local development, setting up a proxy in
vite.config.ts
will help your frontend talk to the agent backend smoothly. 6. Authentication: Don't forget to generate a valid participant token from your backend server to authenticate your connection securely. If you'd like, I’m here to help you with token generation or deploying your frontend—just let me know! Sources: OpenAI and LiveKit partner to turn Advanced Voice into an API | ivr-nagivator/frontend/package.json | complex-agents/ivr-agent/ivr-agent-frontend/README.md | README.md | Client SDKs | livekit/livekit_composite | DeepWiki
f
from where did I got -participant-token this?
t
I'd be happy to guide you through generating the participant token! You can do this either with the LiveKit CLI or a server-side SDK. Let’s walk through both options: 1. Using the LiveKit CLI Simply run this command:
Copy code
lk token create \
--api-key <your_api_key> \
--api-secret <your_api_secret> \
--identity <participant_identity> \
--room <room_name> \
--join \
--valid-for 1h
This will create a token valid for 1 hour, letting the participant join the room you specify. 2. Using Server-Side SDKs Here’s a friendly example in Node.js:
Copy code
import { AccessToken, VideoGrant } from 'livekit-server-sdk';

const at = new AccessToken('api-key', 'secret-key', {
  identity: 'participant-identity',
  name: 'Participant Name',
  ttl: '15m',
});

const videoGrant = {
  room: 'room-name',
  roomJoin: true,
  canPublish: true,
  canSubscribe: true,
};

at.addGrant(videoGrant);
const token = await at.toJwt();
This token lets the participant join, publish, and subscribe in the specified room with the permissions you set. Here are the key parameters you’ll need: • API Key & Secret: These sign the token, so please keep them safe and private. • Identity: A unique ID for your participant. • Room Name: The room the participant will join. • Grants: Permissions like
roomJoin
,
canPublish
, and
canSubscribe
. If you’d like, I’m here to help you set up either the CLI or SDK for generating your tokens—just let me know! Sources: CLI Tools & Token Management | livekit/livekit_composite | DeepWiki | LiveKit CLI Setup | LiveKit Docs | examples/rpc/index.ts | ivr-nagivator/flask_livekit/extension.py | Authentication | LiveKit Docs
f
correct this file for getting access token according to this requirements file also tell if any module I have to install
t
I'd be happy to help! Could you please share the file content you'd like me to review for generating the access token? This way, I can make sure the code fits your needs perfectly and also check if there are any extra modules you might need to install. Looking forward to your reply!