-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature]: Integrate LiveKit for meetings (#2818)
* feat: livekit integration * fix: remove value for livekit in .env * fix: ensure consistent hook usage and optimize state handling * fix: deep scan * fix: Codacy Static Code Analysis * feat: meet type for use * Update .env * Update useCollaborative.ts --------- Co-authored-by: DevOps <[email protected]>
- Loading branch information
1 parent
6cd2d96
commit 1a72239
Showing
16 changed files
with
854 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"use client"; | ||
|
||
import { useAuthenticateUser } from '@app/hooks'; | ||
import { withAuthentication } from 'lib/app/authenticator'; | ||
import { BackdropLoader, Meta } from 'lib/components'; | ||
import dynamic from 'next/dynamic'; | ||
import { useRouter, useSearchParams } from 'next/navigation'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { useTokenLiveKit } from '@app/hooks/useLiveKit'; | ||
|
||
const LiveKit = dynamic(() => import('lib/features/integrations/livekit'), { | ||
ssr: false, | ||
loading: () => <BackdropLoader show /> | ||
}); | ||
|
||
function LiveKitPage() { | ||
const router = useRouter(); | ||
const { user } = useAuthenticateUser(); | ||
const [roomName, setRoomName] = useState<string | undefined>(undefined); | ||
const params = useSearchParams(); | ||
|
||
const onLeave = useCallback(() => { | ||
router.push('/'); | ||
}, [router]); | ||
|
||
useEffect(() => { | ||
const room = params.get("roomName"); | ||
if (room) { | ||
setRoomName(room); | ||
} | ||
}, [params]); | ||
|
||
const { token } = useTokenLiveKit({ | ||
roomName: roomName || '', | ||
username: user?.email || '', | ||
}); | ||
|
||
return ( | ||
<> | ||
<Meta title="Meet" /> | ||
{token && roomName && <LiveKit | ||
token={token!} | ||
roomName={roomName} | ||
liveKitUrl={process.env.NEXT_PUBLIC_LIVEKIT_URL || ''} | ||
onLeave={onLeave} | ||
userChoices={{ | ||
videoEnabled: true, | ||
audioEnabled: true, | ||
audioDeviceId: '', | ||
username: user?.email || '', | ||
videoDeviceId: '' | ||
}} | ||
/>} | ||
</> | ||
); | ||
} | ||
|
||
export default withAuthentication(LiveKitPage, { | ||
displayName: 'LiveKitPage', | ||
showPageSkeleton: false | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import LiveKitPage from './component' | ||
import React from 'react' | ||
|
||
function Page() { | ||
return <LiveKitPage /> | ||
|
||
} | ||
|
||
export default Page |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { AccessToken } from "livekit-server-sdk"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export async function GET(req: NextRequest) { | ||
const room = req.nextUrl.searchParams.get("roomName"); | ||
const username = req.nextUrl.searchParams.get("username"); | ||
|
||
if (!room || typeof room !== 'string' || room.trim() === '') { | ||
return NextResponse.json( | ||
{ error: 'Missing or invalid "roomName" query parameter' }, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
if (!username || typeof username !== 'string' || username.trim() === '') { | ||
return NextResponse.json( | ||
{ error: 'Missing or invalid "username" query parameter' }, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
const apiKey = process.env.LIVEKIT_API_KEY; | ||
const apiSecret = process.env.LIVEKIT_API_SECRET; | ||
const wsUrl = process.env.NEXT_PUBLIC_LIVEKIT_URL; | ||
|
||
if (!apiKey || !apiSecret || !wsUrl) { | ||
console.error("Server misconfigured: missing environment variables."); | ||
return NextResponse.json( | ||
{ error: "Server misconfigured" }, | ||
{ status: 500 } | ||
); | ||
} | ||
|
||
try { | ||
const at = new AccessToken(apiKey, apiSecret, { identity: username }); | ||
at.addGrant({ room, roomJoin: true, canPublish: true, canSubscribe: true, roomRecord: true }); | ||
const token = await at.toJwt(); | ||
return NextResponse.json({ token: token }); | ||
} catch (error) { | ||
console.error("Failed to generate token:", error); | ||
return NextResponse.json( | ||
{ error: "Failed to generate token" }, | ||
{ status: 500 } | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"use client"; | ||
import { tokenLiveKitRoom } from "@app/services/server/livekitroom"; | ||
import { useEffect, useState } from "react"; | ||
|
||
interface ITokenLiveKitProps { | ||
roomName: string; | ||
username: string; | ||
} | ||
|
||
export function useTokenLiveKit({ roomName, username }: ITokenLiveKitProps) { | ||
|
||
const [token, setToken] = useState<string | null>(() => { | ||
if (typeof window !== 'undefined') { | ||
return window.localStorage.getItem('token-live-kit'); | ||
} | ||
return null; | ||
}); | ||
|
||
useEffect(() => { | ||
const fetchToken = async () => { | ||
try { | ||
const response = await tokenLiveKitRoom({ roomName, username }); | ||
window.localStorage.setItem('token-live-kit', response.token); | ||
setToken(response.token); | ||
} catch (error) { | ||
console.error('Failed to fetch token:', error); | ||
} | ||
}; | ||
fetchToken(); | ||
}, [roomName, username, token]); | ||
|
||
return { token }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { JwtPayload } from "jsonwebtoken"; | ||
import { LocalAudioTrack, LocalVideoTrack } from 'livekit-client'; | ||
|
||
export interface ILiveKiteCredentials { | ||
ttl?:number|string | ||
roomName?: string, | ||
identity?: string, | ||
username?: string, | ||
metadata?: string | ||
} | ||
|
||
export interface CustomJwtPayload extends JwtPayload { | ||
exp?: number; | ||
iss?: string; | ||
nbf?: number; | ||
sub?: string; | ||
video?: { | ||
canPublish: boolean; | ||
canPublishData: boolean; | ||
canSubscribe: boolean; | ||
room: string; | ||
roomJoin: boolean; | ||
}; | ||
} | ||
|
||
|
||
|
||
|
||
export interface SessionProps { | ||
roomName: string; | ||
identity: string; | ||
audioTrack?: LocalAudioTrack; | ||
videoTrack?: LocalVideoTrack; | ||
region?: string; | ||
turnServer?: RTCIceServer; | ||
forceRelay?: boolean; | ||
} | ||
|
||
export interface TokenResult { | ||
identity: string; | ||
accessToken: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ILiveKiteCredentials } from "@app/interfaces"; | ||
|
||
export async function tokenLiveKitRoom({ roomName, username }: ILiveKiteCredentials) { | ||
try { | ||
const response = await fetch(`/api/livekit?roomName=${roomName ?? 'default'}&username=${username ?? 'employee'}`); | ||
return await response.json(); | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use client'; | ||
import React from 'react'; | ||
import { | ||
LiveKitRoom, | ||
VideoConference, | ||
formatChatMessageLinks, | ||
LocalUserChoices, | ||
} from '@livekit/components-react'; | ||
import { RoomConnectOptions } from 'livekit-client'; | ||
import "@livekit/components-styles"; | ||
|
||
type ActiveRoomProps = { | ||
userChoices: LocalUserChoices; | ||
roomName?: string; | ||
region?: string; | ||
token?: string; | ||
liveKitUrl?: string; | ||
onLeave?: () => void; | ||
}; | ||
|
||
export default function LiveKitPage({ | ||
userChoices, | ||
onLeave, | ||
token, | ||
liveKitUrl, | ||
}: ActiveRoomProps) { | ||
const connectOptions = React.useMemo((): RoomConnectOptions => ({ | ||
autoSubscribe: true, | ||
}), []); | ||
const LiveKitRoomComponent = LiveKitRoom as React.ElementType; | ||
return ( | ||
<LiveKitRoomComponent | ||
className='!bg-dark--theme-light' | ||
connectOptions={connectOptions} | ||
audio={userChoices.audioEnabled} | ||
video={userChoices.videoEnabled} | ||
token={token} | ||
serverUrl={liveKitUrl} | ||
connect={true} | ||
data-lk-theme="default" | ||
style={{ height: '100dvh' }} | ||
onDisconnected={onLeave} | ||
> | ||
<VideoConference | ||
chatMessageFormatter={formatChatMessageLinks} | ||
SettingsComponent={undefined} // or provide an actual component if needed | ||
/> | ||
</LiveKitRoomComponent> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.