Skip to content

Commit fb3ec22

Browse files
1egomanbcherry
andauthored
Update readme to include agents sdk example (#1219)
Co-authored-by: Ben Cherry <[email protected]>
1 parent 964e13e commit fb3ec22

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

README.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,88 @@ Use this SDK to add realtime video, audio and data features to your React app. B
2121

2222
![LiveKit Components Preview](./.github/assets/livekit-meet.jpg)
2323

24-
## Quick Start
24+
## Agents Quick Start
25+
26+
First add the library to your project:
27+
28+
```shell
29+
npm i @livekit/components-react
30+
```
31+
32+
Next, you need a running agent. If you don't already have one, [it only takes a few minutes to set one up](https://docs.livekit.io/agents/start/voice-ai).
33+
34+
The rest of this guide assumes your agent is configured for [explicit dispatch](https://docs.livekit.io/agents/worker/agent-dispatch/#explicit) with `agent_name="example-agent"`.
35+
36+
Then, you can use the agents sdk to connect and interact with your agent:
37+
38+
```tsx
39+
import { useEffect, useState } from "react";
40+
import { TokenSource } from "livekit-client";
41+
import {
42+
useSession,
43+
useAgent,
44+
SessionProvider,
45+
VideoTrack,
46+
StartAudio,
47+
RoomAudioRenderer,
48+
} from "@livekit/components-react";
49+
50+
// Generated credentials manually and put them here
51+
// Or, generate them another way: https://github.com/livekit/client-sdk-js?tab=readme-ov-file#generating-a-urltoken-with-tokensource
52+
const tokenSource = TokenSource.literal({
53+
serverUrl: "wss://my-livekit-server",
54+
participantToken: 'generated-jwt',
55+
});
56+
57+
export default function Example() {
58+
const session = useSession(tokenSource, {
59+
agentName: 'example-agent', /* <== Put your agent name here! */
60+
});
61+
62+
const toggleStarted = () => {
63+
if (session.connectionState === 'disconnected') {
64+
session.start();
65+
} else {
66+
session.end();
67+
}
68+
};
69+
70+
const agent = useAgent(session);
71+
72+
return (
73+
<SessionProvider session={session}>
74+
<button onClick={toggleStarted} disabled={session.connectionState === 'connecting'}>
75+
{session.isConnected ? 'Disconnect' : 'Connect'}
76+
</button>
77+
78+
{session.isConnected ? (
79+
<div className="flex flex-col gap-4 p-4">
80+
<span>
81+
<strong>Connection State:</strong>
82+
{session.connectionState}
83+
</span>
84+
<span>
85+
<strong>Agent State:</strong>
86+
{agent.state}
87+
</span>
88+
89+
{session.local.cameraTrack ? (
90+
<VideoTrack trackRef={session.local.cameraTrack} />
91+
) : null}
92+
{agent.cameraTrack ? (
93+
<VideoTrack trackRef={agent.cameraTrack} />
94+
) : null}
95+
96+
<StartAudio label="Start audio" />
97+
<RoomAudioRenderer />
98+
</div>
99+
) : null}
100+
</SessionProvider>
101+
);
102+
}
103+
```
104+
105+
### Video Conference Quick Start
25106

26107
First add the library to your project:
27108

0 commit comments

Comments
 (0)