Server Setup for Groups
The backend coordinates everything: it creates sessions, generates JWT tokens, and exposes routes for the client to call. The sample uses the Vonage Node SDK so you can stay in JavaScript/TypeScript on both sides.
Install the SDK (and other dependencies) inside your server project:
npm install npm install -s @vonage/server-sdk
Copy .envcopy to .env and populate it with the Vonage Application ID, API key/secret, and path to your private key. The learning server expects those values and exposes them when deployed through Code Hub.
Session Creation and Tokens
The createSession helper below is representative of what the sample server does:
Session Helper Code
// routes/index.js
async function createSession(response, roomName, sessionProperties = {}, role = 'moderator') {
let sessionId;
let token;
console.log(`Creating ${role} creds for ${roomName}`);
if (roomToSessionIdDictionary[roomName]) {
sessionId = roomToSessionIdDictionary[roomName];
} else {
const session = await vonage.video.createSession(sessionProperties);
roomToSessionIdDictionary[roomName] = session.sessionId;
sessionId = session.sessionId;
}
token = vonage.video.generateClientToken(sessionId, { role });
response.setHeader('Content-Type', 'application/json');
response.send({
applicationId: appId,
sessionId,
token,
});
}
Session Notes
- Sessions are cached in memory to keep everyone in the same room on subsequent requests.
vonage.video.createSession()talks to the Video API and provisions a media router if one does not exist yet.vonage.video.generateClientToken()produces a JWT that includes role information (here we default tomoderatorso the user can control archives later).
The handler returns the Application ID, Session ID, and token back to the browser so it can initialize the SDK.