Last active
October 23, 2020 11:41
-
-
Save RusseII/a0ad81cbe71045ba4602b748d104f4d9 to your computer and use it in GitHub Desktop.
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
import { NowRequest, NowResponse } from '@vercel/node'; | |
const AccessToken = require('twilio').jwt.AccessToken; | |
const VideoGrant = AccessToken.VideoGrant; | |
const fourHours = 14400; | |
const MAX_ALLOWED_SESSION_DURATION = fourHours; | |
const twilioAccountSid = process.env.TWILIO_ACCOUNT_SID; | |
const twilioApiKeySID = process.env.TWILIO_API_KEY_SID; | |
const twilioApiKeySecret = process.env.TWILIO_API_KEY_SECRET; | |
const client = require('twilio')(twilioApiKeySID, twilioApiKeySecret, { accountSid: twilioAccountSid }); | |
const createRoom = async (roomName: string | string[]) => { | |
try { | |
// add logic here for deciding the needed room type and if recording should be on or off | |
const room = await client.video.rooms.create({ | |
recordParticipantsOnConnect: true, | |
statusCallback: 'https://a.deephire.com/v1/live/events', | |
type: 'group', | |
uniqueName: roomName, | |
}); | |
console.log('created room', room) | |
} catch (err) { | |
console.log('error creating room', err); | |
} | |
}; | |
interface QueryInterface { | |
roomName: string; | |
identity: string; | |
} | |
export default async (request: NowRequest, response: NowResponse) => { | |
const { identity, roomName } = request.query; | |
await createRoom(roomName); | |
const token = new AccessToken(twilioAccountSid, twilioApiKeySID, twilioApiKeySecret, { | |
ttl: MAX_ALLOWED_SESSION_DURATION, | |
}); | |
token.identity = identity; | |
const videoGrant = new VideoGrant({ room: roomName }); | |
token.addGrant(videoGrant); | |
response.status(200).send(token.toJwt()); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello ,
Thank you for the code insights. I'm coming from your post about twilio video react app issue #329.
Don't you miss a semicolon at the end of line 37?