Server Archiving Handlers

Starting and stopping an archive is a privileged operation and must happen on the server via the Vonage Video API. The learning server exposes lightweight routes that wrap the Node SDK calls.

Start and Stop Routes

// 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.

Deliver the Playback URL

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.