Add webhook receiver to Node application server

This commit is contained in:
pabloFuente 2024-05-13 21:43:30 +02:00
parent 54554f8c50
commit f1d2f8487a

View File

@ -1,16 +1,24 @@
import "dotenv/config";
import express from "express";
import cors from "cors";
import { AccessToken } from "livekit-server-sdk";
import { WebhookReceiver } from "livekit-server-sdk";
const SERVER_PORT = process.env.SERVER_PORT || 6080;
const LIVEKIT_API_KEY = process.env.LIVEKIT_API_KEY || "devkey";
const LIVEKIT_API_SECRET = process.env.LIVEKIT_API_SECRET || "secret";
const webhookReceiver = new WebhookReceiver(
LIVEKIT_API_KEY,
LIVEKIT_API_SECRET
);
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.raw({ type: "application/webhook+json" }));
app.post("/token", async (req, res) => {
const roomName = req.body.roomName;
@ -29,6 +37,18 @@ app.post("/token", async (req, res) => {
res.json(token);
});
app.post("/webhook", async (req, res) => {
try {
const event = await webhookReceiver.receive(
req.body,
req.get("Authorization")
);
console.log(event);
} catch (error) {
console.error("Error validating webhook event", error);
}
});
app.listen(SERVER_PORT, () => {
console.log("Server started on port:", SERVER_PORT);
});