Server Archiving Handlers and Playback
Here you wire the backend routes that turn UI recording actions into real archive behavior.
If you need the full route details, use Server archiving handlers while wiring and verifying these routes.
You will handle two route groups: start/stop handlers for archive lifecycle and a view/playback handler for archive retrieval.
Start/stop archive handlers
// routes/index.js
router.post('/archive/start', async function (req, res) {
console.log('attempting to start archive');
const { sessionId } = req.body;
try {
const archive = await vonage.video.startArchive(sessionId, {
name: findRoomFromSessionId(sessionId),
});
res.setHeader('Content-Type', 'application/json');
res.send(archive);
} catch (error) {
console.error('error starting archive: ', error);
res.status(500).send({ error: 'startArchive error:' + error });
}
});
router.post('/archive/:archiveId/stop', async function (req, res) {
const { archiveId } = req.params;
console.log('attempting to stop archive: ' + archiveId);
try {
const archive = await vonage.video.stopArchive(archiveId);
res.setHeader('Content-Type', 'application/json');
res.send(archive);
} catch (error) {
console.error('error stopping archive: ', error);
res.status(500).send({ error: 'stopArchive error:', error });
}
});
Both routes return the archive object so the client can persist the ID and react to archiveStarted / archiveStopped events.
These handlers should return stable status and archive identifiers so the UI can track progress safely.
Playback/view handler
The archive files live in Vonage-managed storage. Expose a simple view route that verifies the archive is available before redirecting:
router.get('/archive/:archiveId/view', async function (req, res) {
const { archiveId } = req.params;
console.log('attempting to view archive: ' + archiveId);
try {
const archive = await vonage.video.getArchive(archiveId);
if (archive.status === 'available') {
res.redirect(archive.url);
} else {
res.render('view', { title: 'Archiving Pending' });
}
} catch (error) {
console.log('error viewing archive: ', error);
res.status(500).send({ error: 'viewArchive error:' + error });
}
});
When the client clicks View Archive from the UI, it hits this route. If the archive is still being processed, the user sees a friendly “pending” message. Once the archive transitions to available, the server redirects the browser to the hosted MP4 so it can play natively.
This route completes the lifecycle by exposing playback only when recording is actually ready.
Check before the archiving integration exercise
- Archive IDs/statuses are returned consistently.
- Pending archives do not break flow.
- Available archives redirect to playback correctly.