|
| 1 | +import { RoomConfiguration } from '@livekit/protocol'; |
| 2 | +import bodyParser from 'body-parser'; |
1 | 3 | import dotenv from 'dotenv';
|
2 | 4 | import express from 'express';
|
3 | 5 | import { AccessToken } from 'livekit-server-sdk';
|
4 | 6 |
|
5 | 7 | type TokenRequest = {
|
6 |
| - roomName: string; |
7 |
| - participantName: string; |
| 8 | + room_name?: string; |
| 9 | + participant_name?: string; |
| 10 | + participant_identity?: string; |
| 11 | + participant_metadata?: string; |
| 12 | + participant_attributes?: Record<string, string>; |
| 13 | + room_config?: ReturnType<RoomConfiguration['toJson']>; |
| 14 | + |
| 15 | + // (old fields, here for backwards compatibility) |
| 16 | + roomName?: string; |
| 17 | + participantName?: string; |
8 | 18 | };
|
9 | 19 |
|
10 | 20 | // Load environment variables from .env.local file
|
11 | 21 | dotenv.config({ path: '.env.local' });
|
12 | 22 |
|
13 | 23 | // This route handler creates a token for a given room and participant
|
14 |
| -async function createToken({ roomName, participantName }: TokenRequest) { |
| 24 | +async function createToken(request: TokenRequest) { |
| 25 | + const roomName = request.room_name ?? request.roomName!; |
| 26 | + const participantName = request.participant_name ?? request.participantName!; |
| 27 | + |
15 | 28 | const at = new AccessToken(process.env.LIVEKIT_API_KEY, process.env.LIVEKIT_API_SECRET, {
|
16 | 29 | identity: participantName,
|
17 | 30 | // Token to expire after 10 minutes
|
18 | 31 | ttl: '10m',
|
19 | 32 | });
|
| 33 | + |
20 | 34 | // Token permissions can be added here based on the
|
21 | 35 | // desired capabilities of the participant
|
22 | 36 | at.addGrant({
|
23 | 37 | roomJoin: true,
|
24 | 38 | room: roomName,
|
25 | 39 | canUpdateOwnMetadata: true,
|
26 | 40 | });
|
| 41 | + |
| 42 | + if (request.participant_identity) { |
| 43 | + at.identity = request.participant_identity; |
| 44 | + } |
| 45 | + if (request.participant_metadata) { |
| 46 | + at.metadata = request.participant_metadata; |
| 47 | + } |
| 48 | + if (request.participant_attributes) { |
| 49 | + at.attributes = request.participant_attributes; |
| 50 | + } |
| 51 | + if (request.room_config) { |
| 52 | + at.roomConfig = RoomConfiguration.fromJson(request.room_config); |
| 53 | + } |
| 54 | + |
27 | 55 | return at.toJwt();
|
28 | 56 | }
|
29 | 57 |
|
30 | 58 | const app = express();
|
| 59 | +app.use(bodyParser.json()); |
31 | 60 | const port = 3000;
|
32 | 61 |
|
33 | 62 | app.post('/createToken', async (req, res) => {
|
34 |
| - const { roomName = 'demo-room', participantName = 'demo-user' } = req.body ?? {}; |
35 |
| - res.send(await createToken({ roomName, participantName })); |
| 63 | + const body = req.body ?? {}; |
| 64 | + body.roomName = body.roomName ?? `room-${crypto.randomUUID()}`; |
| 65 | + body.participantName = body.participantName ?? `user-${crypto.randomUUID()}`; |
| 66 | + |
| 67 | + try { |
| 68 | + res.send({ |
| 69 | + server_url: process.env.LIVEKIT_URL, |
| 70 | + participant_token: await createToken(body), |
| 71 | + }); |
| 72 | + } catch (err) { |
| 73 | + console.error('Error generating token:', err); |
| 74 | + res.status(500).send({ message: 'Generating token failed' }); |
| 75 | + } |
36 | 76 | });
|
37 | 77 |
|
38 | 78 | app.listen(port, () => {
|
|
0 commit comments