const liveKitApi = require('livekit-server-sdk');
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const AccessToken = liveKitApi.AccessToken;
const RoomServiceClient = liveKitApi.RoomServiceClient;
const port = 8280;
const hostaddress = '0.0.0.0';
const apiKey = 'api-key';
const secretKey = 'secret-key';
const livekitHost = 'http://${hostaddress}:'+port;
const svc = new RoomServiceClient(livekitHost,apiKey,secretKey);
// const Room = liveKitApi.Room;
var roomName = 'name-of-room';
var participantName = 'user-name';
// const at = new AccessToken(apiKey, secretKey, {
// identity: participantName,
// });
// at.addGrant({ roomJoin: true, room: roomName });
// const token = at.toJwt();
// console.log('access token', token);
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.get("/getToken", async (req, res,next) => {
console.log(req.body.username);
participantName = req.body.username;
const at = new AccessToken(apiKey, secretKey, {
identity: participantName,
ttl: '10 days'
});
at.addGrant({
roomJoin: true,
room: roomName,
canPublish: true,
canPublishData: true,
canSubscribe: true,
});
const token = at.toJwt();
// create a new room
// const opts = {
// name: roomName,
// // timeout in seconds
// emptyTimeout: 10 * 60,
// maxParticipants: 20,
// };
// svc.createRoom(opts).then((room) => {
// console.log('room created' + room);
// }).catch((error) =>{
// console.log('error in room creating'+ error);
// });
res.json({
// ok: true,
// data: {
token: token
// participantName: participantName,
// roomName: roomName,
// },
});
});
// Error Cofiguration of app
app.use((req, res, next) => {
const error = new Error('not found')
error.status = 404
next(error)
})
app.use((error, req, res, next) => {
res.status(error.status || 500)
res.json({
error: {
message: error.message
}
})
})
app.listen(port, hostaddress,() => {
console.log(
:zap:️[server]: Server is running at http://${hostaddress}:${port}
);
});