Skip to content

Commit 99b6fda

Browse files
authored
In the examples, add a helper function to generate a random user identity (#691)
1 parent 5337dd5 commit 99b6fda

File tree

11 files changed

+32
-22
lines changed

11 files changed

+32
-22
lines changed

Diff for: .changeset/tame-nails-deny.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@livekit/component-example-next": patch
3+
---
4+
5+
Add helper function to generate random user identity

Diff for: examples/nextjs/lib/helper.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* A simple helper function to generate a random user identity.
3+
*/
4+
export function generateRandomUserId() {
5+
return `test-identity-${String(Math.random() * 100_000).slice(0, 5)}` as const;
6+
}

Diff for: examples/nextjs/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"react-dom": "^18.2.0"
1919
},
2020
"devDependencies": {
21-
"@faker-js/faker": "^7.6.0",
2221
"@types/node": "^18.6.5",
2322
"@types/react": "^18.0.17",
2423
"@types/react-dom": "^18.0.6",

Diff for: examples/nextjs/pages/audio-only.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { AudioConference, LiveKitRoom, useToken } from '@livekit/components-react';
22
import type { NextPage } from 'next';
3+
import { generateRandomUserId } from '../lib/helper';
34

45
const AudioExample: NextPage = () => {
56
const params = typeof window !== 'undefined' ? new URLSearchParams(location.search) : null;
67
const roomName = params?.get('room') ?? 'test-room';
7-
const userIdentity = params?.get('user') ?? 'test-identity';
8+
const userIdentity = params?.get('user') ?? generateRandomUserId();
89

910
const token = useToken(process.env.NEXT_PUBLIC_LK_TOKEN_ENDPOINT, roomName, {
1011
userInfo: {

Diff for: examples/nextjs/pages/clubhouse.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ import {
1414
import styles from '../styles/Clubhouse.module.scss';
1515
import { Track } from 'livekit-client';
1616
import { useMemo, useState } from 'react';
17+
import { generateRandomUserId } from '../lib/helper';
1718

1819
const Clubhouse = () => {
1920
const params = typeof window !== 'undefined' ? new URLSearchParams(location.search) : null;
2021
const roomName = params?.get('room') ?? 'test-room';
21-
const userIdentity = params?.get('user') ?? 'test-identity';
22+
const userIdentity = params?.get('user') ?? generateRandomUserId();
2223

2324
const token = useToken(process.env.NEXT_PUBLIC_LK_TOKEN_ENDPOINT, roomName, {
2425
userInfo: {

Diff for: examples/nextjs/pages/customize.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ import myStyles from '../styles/Customize.module.css';
1717
import type { NextPage } from 'next';
1818
import { HTMLAttributes, useState } from 'react';
1919
import { isTrackReference } from '@livekit/components-core';
20+
import { generateRandomUserId } from '../lib/helper';
2021

2122
const CustomizeExample: NextPage = () => {
2223
const params = typeof window !== 'undefined' ? new URLSearchParams(location.search) : null;
2324
const roomName = params?.get('room') ?? 'test-room';
24-
const userIdentity = params?.get('user') ?? 'test-identity';
25+
const userIdentity = params?.get('user') ?? generateRandomUserId();
2526
const token = useToken(process.env.NEXT_PUBLIC_LK_TOKEN_ENDPOINT, roomName, {
2627
userInfo: {
2728
identity: userIdentity,
@@ -85,8 +86,8 @@ export function Stage() {
8586
{isTrackReference(track) ? <VideoTrack {...track} /> : <p>Camera placeholder</p>}
8687
<div className={myStyles['participant-indicators']}>
8788
<div style={{ display: 'flex' }}>
88-
<TrackMutedIndicator source={Track.Source.Microphone}></TrackMutedIndicator>
89-
<TrackMutedIndicator source={track.source}></TrackMutedIndicator>
89+
<TrackMutedIndicator source={Track.Source.Microphone} />
90+
<TrackMutedIndicator source={track.source} />
9091
</div>
9192
{/* Overwrite styles: By passing class names, we can easily overwrite/extend the existing styles. */}
9293
{/* In addition, we can still specify a style attribute and further customize the styles. */}

Diff for: examples/nextjs/pages/e2ee.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import { LiveKitRoom, useToken, VideoConference } from '@livekit/components-reac
33
import type { NextPage } from 'next';
44
import * as React from 'react';
55
import { Room, ExternalE2EEKeyProvider } from 'livekit-client';
6+
import { generateRandomUserId } from '../lib/helper';
67

78
const E2EEExample: NextPage = () => {
89
const params = typeof window !== 'undefined' ? new URLSearchParams(location.search) : null;
910
const roomName = params?.get('room') ?? 'test-room';
10-
const userIdentity = params?.get('user') ?? 'test-identity';
11+
const userIdentity = params?.get('user') ?? generateRandomUserId();
1112
setLogLevel('warn', { liveKitClientLogLevel: 'debug' });
1213

1314
const token = useToken(process.env.NEXT_PUBLIC_LK_TOKEN_ENDPOINT, roomName, {

Diff for: examples/nextjs/pages/index.tsx

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
import type { NextPage } from 'next';
22
import Head from 'next/head';
33
import styles from '../styles/Home.module.scss';
4-
import { faker } from '@faker-js/faker';
54

65
const EXAMPLE_ROUTES = {
7-
minimal: { title: 'Minimal example', href: () => `/minimal?user=${faker.name.fullName()}` },
8-
simple: { title: 'Simple example', href: () => `/simple?user=${faker.name.fullName()}` },
6+
minimal: { title: 'Minimal example', href: () => `/minimal` },
7+
simple: { title: 'Simple example', href: () => `/simple` },
98
audioOnly: {
109
title: 'Audio only example',
11-
href: () => `/audio-only?user=${faker.name.fullName()}`,
10+
href: () => `/audio-only`,
1211
},
1312
customize: {
1413
title: 'Simple example with custom components',
15-
href: () => `/customize?user=${faker.name.fullName()}`,
14+
href: () => `/customize`,
1615
},
1716
clubhouse: {
1817
title: 'Clubhouse clone build with LiveKit components',
19-
href: () => `/clubhouse?user=${faker.name.fullName()}`,
18+
href: () => `/clubhouse`,
2019
},
2120
processors: {
2221
title: 'Minimal example with background blur',
23-
href: () => `/processors?user=${faker.name.fullName()}`,
22+
href: () => `/processors`,
2423
},
2524
} as const;
2625

Diff for: examples/nextjs/pages/minimal.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { setLogLevel } from '@livekit/components-core';
22
import { LiveKitRoom, useToken, VideoConference } from '@livekit/components-react';
33
import { RoomConnectOptions } from 'livekit-client';
44
import type { NextPage } from 'next';
5+
import { generateRandomUserId } from '../lib/helper';
56

67
const MinimalExample: NextPage = () => {
78
const params = typeof window !== 'undefined' ? new URLSearchParams(location.search) : null;
89
const roomName = params?.get('room') ?? 'test-room';
9-
const userIdentity = params?.get('user') ?? 'test-identity';
10+
const userIdentity = params?.get('user') ?? generateRandomUserId();
1011
setLogLevel('info', { liveKitClientLogLevel: 'warn' });
1112

1213
const token = useToken(process.env.NEXT_PUBLIC_LK_TOKEN_ENDPOINT, roomName, {

Diff for: examples/nextjs/pages/simple.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@ import { Track } from 'livekit-client';
1313
import type { NextPage } from 'next';
1414
import { useState } from 'react';
1515
import styles from '../styles/Simple.module.css';
16+
import { generateRandomUserId } from '../lib/helper';
1617

1718
const SimpleExample: NextPage = () => {
1819
const params = typeof window !== 'undefined' ? new URLSearchParams(location.search) : null;
1920
const roomName = params?.get('room') ?? 'test-room';
20-
const userIdentity = params?.get('user') ?? 'test-identity';
21+
const userIdentity = params?.get('user') ?? generateRandomUserId();
2122
const [connect, setConnect] = useState(false);
2223
const [isConnected, setIsConnected] = useState(false);
2324

2425
const token = useToken(process.env.NEXT_PUBLIC_LK_TOKEN_ENDPOINT, roomName, {
2526
userInfo: {
2627
identity: userIdentity,
27-
name: 'my-name',
28+
name: userIdentity,
2829
},
2930
});
3031

Diff for: yarn.lock

-5
Original file line numberDiff line numberDiff line change
@@ -1767,11 +1767,6 @@
17671767
resolved "https://registry.yarnpkg.com/@eslint/js/-/js-8.53.0.tgz#bea56f2ed2b5baea164348ff4d5a879f6f81f20d"
17681768
integrity sha512-Kn7K8dx/5U6+cT1yEhpX1w4PCSg0M+XyRILPgvwcEBjerFWCwQj5sbr3/VmxqV0JGHCBCzyd6LxypEuehypY1w==
17691769

1770-
"@faker-js/faker@^7.6.0":
1771-
version "7.6.0"
1772-
resolved "https://registry.yarnpkg.com/@faker-js/faker/-/faker-7.6.0.tgz#9ea331766084288634a9247fcd8b84f16ff4ba07"
1773-
integrity sha512-XK6BTq1NDMo9Xqw/YkYyGjSsg44fbNwYRx7QK2CuoQgyy+f1rrTDHoExVM5PsyXCtfl2vs2vVJ0MN0yN6LppRw==
1774-
17751770
"@fal-works/esbuild-plugin-global-externals@^2.1.2":
17761771
version "2.1.2"
17771772
resolved "https://registry.yarnpkg.com/@fal-works/esbuild-plugin-global-externals/-/esbuild-plugin-global-externals-2.1.2.tgz#c05ed35ad82df8e6ac616c68b92c2282bd083ba4"

0 commit comments

Comments
 (0)