Create a Party With Ruby on Rails and the Vonage Video API – Part 1

Published May 12, 2020 by Ben Greenberg

My son recently celebrated his 9th birthday, and like many kids his age, he was looking forward to his birthday party for months. In the midst of the Covid-19 pandemic, we knew that we needed to do something different this year, so I built him a video watch party app using the Vonage Video API!

You, too, can build your own video watch party app with the Vonage Video API and Ruby on Rails, and I’ll show you how. This two-part series will walk you through the steps to build your full-stack Rails application. The first part will focus on the backend of the app and the second part will focus on the frontend.

tl;dr If you would like to skip ahead and get right to deploying it, you can find all the code for the app on GitHub or click this button to deploy it straight to Heroku.

Deploy Video Watch Party App to Heroku

Table of Contents

What Will the App Do

Before we begin building the application, let’s take a moment to discuss what it will do.

The app will have three distinct views:

1) A Landing Page
2) Party Video Chat
3) Video Watch Party

The entry to the app will be through the landing page. At the landing page, participants will be asked to provide their name and the password for the party. The name will be used to identify them in the text chat. The password will provide a small layer of security for the app.

After participants enter their name and the correct party password, they will be redirected to the Party Video Chat view. In this view, each participant will see and hear each other in a grid format. There will also be a place to chat by text as well. Everyone will see a real-time count of the participants in the navigation bar. The moderator of the party will also see a link to turn the Watch Mode On/Off.

Once the moderator turns the Watch Mode on, all the participants will be directed to the third and final view, which is the Video Watch Party. In this view, the moderator will share their screen in the center of the page. The moderator’s audio is also published in a hidden <div> so the participants can hear the audio from the shared video. The text chat will be the means of communication in this view. The audio and video feeds of all the participants will be disabled.

The moderator can move people between the Party Video Chat and Video Watch Party modes whenever they would like by pressing the Watch Mode On/Off button in their navigation bar.

Now that we have an idea of what we will be building, let’s start building it!


This app requires the following:

It is free to create a Vonage Video API account. You need to do so in order to obtain your API key and secret, which are essential to making the app functional.

Vonage Video API Credentials

After you have created an account with the Vonage Video API, you will see a dashboard interface. The first step in obtaining API credentials is to create a new project.

  • Select the Create New Project link from the left sidebar.
  • Select API when asked what kind of project to create
  • Provide any name for the project
  • Choose the VP8 codec option. (Details on the difference between VP8 and H.264 are detailed here)

You now have access to your project’s API key and secret. Keep them somewhere safe, we will be using them soon.


From your terminal initiate a new Rails application by executing the following:

Once that is done, change into the project directory and open up the project with your preferred code editor.

Gem Dependencies

We will add the Vonage Video API (formerly TokBox OpenTok) Ruby SDK to the Gemfile, along with the dotenv-rails gem to manage environment variables:

Once that is done, we can run bundle install from the command line to install our dependencies.

Model Generation

Next, we will generate a model to hold and manipulate the video session information. From the command line execute the following:

This command will create a model file inside /app/models/ and a database migration file inside /db/migrate/. Let’s open up the database migration file in our code editor. We need to add default values to the columns before we migrate it.

You can find the migration file inside the /db/migrate/ folder. It will be the only file inside the folder, and will look similar to this:

We want to ensure that the session_id is never null and we also want to make sure that the expired boolean defaults to false. In order to do that, modify your migration file by adding , null: false and , default: false to the :session_id and :expired lines, respectively.

You can now commit this database migration to the schema by running rake db:create from the command line. This command will create the PostgreSQL database and the sessions table with the session_id and expired columns.

Routes Definition

The application needs the HTTP routes that it will be accessed defined and pointing to the correct controller methods. Open up the /config/routes.rb file and add the following:

All the routes point to methods inside the VideoController. We will create the controller in the next step.

  • The GET root route directs to the #landing action. This is the route for the landing page.
  • The GET /screenshare route points to the #screenshare action. This is the route for the watch party view.
  • The GET /party route points to the #index action. This is the route for the video chat view.
  • The POST /name route points to the #name action. This is where the landing page form will send its data.
  • The POST /chat/send route points to the #chat action. This is where the text chat messages will be sent.

Lastly in this step, we will create the VideoController.

Controller Generation

In the next step, we will create methods for the controller. In this last Installation step, we will generate the controller file. From the command line execute the following:

This will create a video_controller.rb file inside the /app/controllers/ folder with empty methods for each of the actions we specified in the command. It will also create the basic view structure for the app inside /app/views/video.

Creating the Model and Controller Methods

Now that all the necessary file structure and database schema has been created, it’s time to create the methods for the application. We will need to create methods in both the Video Controller and in the Session model. Let’s start with the Session model first.

Defining the Model Methods

Each Vonage Video session has its own unique session ID. This session ID is what enables different participants to join the same video chat. Additionally, each participant in the video chat is granted a token that enables them to participate. A token can be given special permissions, like moderation capabilities.

In the Session model we are going to create three class methods that will be used to either create a new session ID or load the previous one, and generate tokens for each participant.

The Session#create_or_load_session_id method will check to see if there already is a session ID. If there is an ID, it will use that ID. If not, it will generate a new one. Session IDs can expire, but for the purposes of this tutorial, we are going to work only with active session IDs:

The above method also references an additional method we need to create called Session#create_new_session that does the work of creating a new session if one does not exist:

Lastly, we will create a method that will assign the right token for each participant:

At the top of the model definition, we also need to instantiate an instance of the Vonage Video API (formerly known as TokBox OpenTok) SDK and assign it to an instance variable to use it throughout the model. All together the file will look like the following:

We are now ready to move on to build our controller methods that will manage the routes of the app.

Defining the Controller Methods

The video controller will have a method for each route, and a few helper methods to build out the site. The first method we are going to build will provide all the subsequent methods access to the Video API credentials information.

Open up the video_controller.rb file in /app/controllers and after the class definition add the following method:

As you will see in Part 2 of this series when we build the frontend of the app, these instance variables will also be critical in passing the data from the backend to the frontend of the site.

Next, we will create a method for each of the routes in our application:

As you can see above, the #name method assigns the value of the @name variable taken from the landing page welcome form. It also provides the small layer of gatekeeping for the application, by only redirecting the participant to the video chat page if the password they provided matches the one set in the environment variable. If the password does not match they are redirected to the landing page and asked to try again.

The rest of the methods are empty definitions, just the minimum to provide Rails with the information to seek out the view template corresponding to the name of the action. The only other exception is the #screenshare method that sets a @darkmode instance variable, which will be used to put the site into a dark mode visual setting during the screenshare view.

The #name method also references name_params, which leverages Rails Strong Parameters. We need to build a private method called name_params that defines precisely which parameters the form on the landing page should include. Let’s do that now:

With that private method, we have built out our controller. All together it will look like the following:

Before we go on and create our ERB files for our views, we can take a moment now and define a custom YAML file that will serve as the source of truth for information about the site.

This information will be used to populate data on the site like the name of the party, the welcome message, the language and language direction of the site, and more. Putting this information into a single place will allow us to easily change it in the future without needing to modify multiple files.

Providing Custom Site Configuration

The place in Rails to place custom configuration files is inside the /config folder, so let’s add a site_info.yml file inside there. We will read the data from this file to create the context for our site, things like the name of the party and language of the site:

There are default values provided in the example above. Feel free to edit and change those for the needs of your application.

In order to use this information, we need to load and read it somewhere. We will add several :before_action settings to the ApplicationController that will take in all of this information and make it available throughout the app.

Open up the application_controller.rb file inside the /app/controllers directory and add the following:

Now those instance variables holding the data from the site_info.yml are available to be used inside the view files, which we will create now.

Creating the Views

Defining the Application Layout

The first view we will work with is the default layout for the application. This file can be found at /app/views/layouts/application.html.erb. Inside the view we are goin to add the information about the language of our site, whether to go to dark mode or not, and also load the Video API JS script:

An interesting point you will have noticed in the example above is we also created three JavaScript variables inside <script> tag. We passed the corresponding Ruby instance variable data to those newly instantiated JavaScript variables. In this way, we have started having our backend speak to our frontend.

The rest of the view files we will work with will be the particular views of the app; the landing page, video chat, and video watch views. Before we build those though, let’s create some partials that we will use throughout the rest of the views.

Defining the Partials

Partials are a great way to reuse ERB components throughout the view structure in a Rails application. Instead of defining the same content multiple times, we can put it in one file and simply invoke that file whenever we want to use that content. This application will have three partials; a partial for the header, a partial for the text chat, and a partial for the text chat button icon.

Create a file called _header.html.erb inside /app/viws/video/ and add the following to it:

The header partial reads the data from the @navbar_title instance variable to provide the name of the application. You will also notice an empty <p> tag with an id of #participant-count. That will be populated with data from the JavaScript we will create in Part 2 of this blog post series. Lastly, the header has a <button> tag that will only be visible to the moderator and allows them to switch between chat and screenshare views for all the participants.

Now, create another file called _button-chat.html.erb in the same folder and add the following:

The HTML above generates a text chat icon that participants can click on to reveal or hide the text chatbox.

The last partial is a file that will hold the text chat box area, including the form to submit new chat messages.

Create a file called _chat.html.erb in the same directory and its contents will look like the following:

In the _chat.html.erb partial you will also see another empty tag, this time a <div> with the id of #history. All the text messages will go into that area automatically using the Vonage Video API text message functionality within the Signal API. We will discuss that in Part 2.

Defining the Landing Page

The landing page will be the place that the participants will first encounter when they come to the application. Its purpose is to ask the participants for their name, and the party password to enter the site.

Landing Page View

Create a new file inside /app/views/video called landing_html.erb and add the following:

Similar to the partials, the landing page view leverages the instance variables created in the ApplicationController to generate the welcome message and the text for the form.

Defining the Video Chat View

The video chat view will be the place that the participants will chat with each other with their video cameras and microphones. This view, in addition to the screenshare view, is the two essential parts of the application.

Video Chat Page View

To make this view, create another new file in the same directory called index.html.erb with the following inside of it:

This view has several components that are worth mentioning.

The first is what is happening inside the <script></script> tags. Similar to the application layout, we continue to pass more data to the frontend of the site in the form of new JavaScript variables. Separately, in order to take advantage of these variables inside the site after the JavaScript is loaded, we also add a small function to reload the page if it is the first time it is being loaded in the browser.

The other area we will mention is you will notice that most of the view consists of empty <div> tags. The reason is that those will be populated by the videos from the Video API dynamically. The frontend JavaScript will seek out those tags by their ID names and add the videos of all the participants inside the #subscribers element and add your video to the #publisher element.

Defining the Screenshare View

The final view we need to create for the application is the one for the video screenshare. In this view, the participants can continue chatting via the text chat box, while all watching the same screen together.

Screenshare View

This view will only need to provide the <div> elements for the API to provide one publisher, namely the screenshare video, and one audio feed. A screenshare by itself does not include audio, which would make it difficult to watch a video together. That is why we will also publish an audio feed from the moderator’s computer to accompany the screenshare.

Add a file called screenshare.html.erb inside the same folder with the following:

At this point, the backend of our app is ready! Congratulations, you’ve finished Part 1 of getting the video watch party created.

Next Steps

In Part 2 of this blog post series, we will build the frontend of the application. While the backend of the app was mainly written in Ruby and leveraged the Vonage Video API Ruby SDK, the frontend will be written in JavaScript and utilize the JavaScript SDK.

The work of providing the data that the JavaScript SDK will need has already transpired in the backend we created. Now we need to build the JavaScript classes and functions that will work with that information. Thanks to advances in Rails and its incorporation of Webpack, there is a clear process for incorporating JavaScript into a Rails application, and we will follow those steps.

Continue on to Part 2 of this blog post series to finish up building the application.

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