Create a Basic Video Chat with Vonage Video API

Published March 30, 2020 by Hui Jing Chen

This series of tutorials will explore the Vonage Video API (formerly TokBox OpenTok) and what you can build with it. The Video API is very robust and highly customizable, and in each post we’ll show how to implement a specific feature using the API, starting with the most basic audio-video chat.

As this application will require some server-side code, we will use Glitch for ease of setup. You can also download the code from this Glitch project and deploy it on your server or hosting platform of choice (may probably require some configuration tweaking based on the requirements of your platform).

We will not be using any frontend frameworks for this series, just vanilla Javascript to keep the focus on the Video API itself. At the end of this tutorial, you should be able to start an audio-video chat with a friend by sharing a link with them.

Screenshot of video chat

All the code for this application can be found in this GitHub repository or remixed on Glitch.


Before we get started, you will need a Vonage Video API account, which you can create for free here. You will also need Node.js installed (if you are not using Glitch).

Create an API Project

After you log into your account, you will see your dashboard interface. You can create a new project by selecting Create New Project from the left sidebar.

You will be presented with 2 options, Embed or API. Choose the API option by clicking the Create Custom Project button.

The next step will ask for your project name and preferred codec. Name your project as you like, and go with the suggested codec option of VP8 for now. Details on the difference between VP8 and H.264 are detailed here.

Once your project is created, you will have access to your API key and secret. Each project will have its own API key and secret.

Setting up on Glitch

If you chose not to use Glitch, you can skip this portion and go straight to the code walkthrough and explanation in the next section.

On the Glitch home page, select New Project from the top right corner of the page, and choose the hello-express option, which gives you a Node application with Express installed.

Glitch landing page

You can install additional libraries via the command line by bringing up the console. Click on the Tools button at the bottom of the left sidebar, then click on Logs.

Screenshot showing the location of the Tools button

Install the Vonage Video API Server SDK via the following command:

Note the use of pnpm instead of npm, which is the package manager of choice on Glitch. It does the same thing as npm.

Screenshot of Glitch terminal after opentok is installed

Once that is done, go ahead and clean out the server.js file to remove the unnecessary placeholder code.

Screenshot of the server.js file on Glitch

Go to the .env file and add your API key and secret from your Vonage Video API project.

Screenshot of the .env file on Glitch

Basic Project Structure

This is what the folder structure looks like when you start up a new Glitch hello-express project:

Folder structure of the project

Our application will be made up of two pages: a landing page for users to create a session (we’ll call this a “Room” and subsequent participants can join that same “Room”), and then the actual video chat page.

Let’s add a landing.html file to the views folder by clicking the New File button in the left sidebar. Name the file views/landing.html and paste the following markup into the page. The page will have a simple form element with an input that allows users to submit a room name. You can use the following markup if you don’t want to write your own from scratch.

Creating a new file in your Glitch project

The index.html page will also be relatively sparse for now, a page with two div elements, one for subscriber and one for publisher. We will go through what these terms mean when we get to the Vonage Video API section of this tutorial.

For the server.js file, let’s clean out the unnecessary placeholder code. You should be left with something like this:

Lastly, let’s also clear out the existing styles inside the style.css file so we can replace them with our own once we have wired everything up.

The general overview of how the Vonage Video API works to connect people and allow them to have a video chat is as follows:

  • Step 1: The session is created by your app server
  • Step 2: A client loads the app and the server creates a token
  • Step 3: The client connects and begins streaming to the session
  • Step 4: A new client connects to the session
  • Step 5: The clients subscribe to each other’s streams

A complete introduction with animated GIFs showing the entire flow is available on the Vonage Video API documentation site.

Initializing a Session

As mentioned at the start, TokBox OpenTok is now Vonage Video API. We haven’t made any changes to our package names, so you will still reference OpenTok in your code. Start off by instantiating an OpenTok object with your API key and secret in the server.js file.

Let’s modify the routes for our two pages so that users see the landing page on first load, which allows them to enter a room name to start a session. Once that is successful, they will be redirected to the main application page where the video will be rendered.

On our landing.html file, let’s add an event handler for form submission which will POST the room name to our server. Because it is four lines of code, let’s add it as a script tag at the bottom of the page.

This means we will have to handle the POST route in the server.js file. The following chunk of code is fairly long, so explanations are included as comments in the code. The gist of it is, this will check for an existing session or create a new one if needed, based on the room name entered from the landing page. Every session has a unique session ID, which allows other people to join the session.

This code will then generate the credentials needed for the client to connect to the session. They comprise of the session ID, a token, and the API key. These credentials will be passed back to the client as a response.

Connecting to the Session, Subscribing and Publishing

Over on the client side, we first create a session object with the API key and session ID from the response. We also need to create a publisher object, which will replace the div element with the id=publisher with the your video feed.

We’ll connect to the session with the session.connect() method, and if successful, we will publish our video stream with the session.publish() method.

We will also listen for the streamCreated event so we can subscribe to users who join the session after we have connected to the session.

When the room name form is submitted, we will make a POST request to the /session/:room route and use the data from the response to run the functionality detailed above. To do that, we will wrap it all up in a function called initializeSession() like so:

Some Styling to Tidy Things Up

The general functionality of our video chat is done, so the only thing left is to add some styles to adjust the layout. The original two div elements (for subscriber and publisher) will have their contents replaced by their respective video feeds.

Markup when publisher is publishing to the session

This is the most barebones layout style to get your publisher video stream to display on the bottom-left corner of the page, while the other people on the stream take up the full background of the page.

Those styles will give you the most basic video chat with the most basic layout, which you already saw at the beginning of this tutorial.

Screenshot of landing page

Screenshot of video chat

You can then proceed to add whatever visual styling like fonts, colours and even logo images if you like, to spruce things up.

What’s Next?

There are additional functionalities we can build with the Vonage Video API which will be covered in future tutorials, but in the meantime, you can find out more at our comprehensive documentation site. If you run into any issues or have questions, reach out to us on our Community Slack. Thanks for reading!

Leave a Reply

Your email address will not be published.

Get the latest posts from Nexmo’s next-generation communications blog delivered to your inbox.

By signing up to our communications blog, you accept our privacy policy , which sets out how we use your data and the rights you have in respect of your data. You can opt out of receiving our updates by clicking the unsubscribe link in the email or by emailing us at