<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chris Corcoran</title>
    <description>The latest articles on DEV Community by Chris Corcoran (@chriscorcoran).</description>
    <link>https://dev.to/chriscorcoran</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F382244%2F48c53c0e-8637-431e-823c-491f21dc9244.jpeg</url>
      <title>DEV Community: Chris Corcoran</title>
      <link>https://dev.to/chriscorcoran</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chriscorcoran"/>
    <language>en</language>
    <item>
      <title>Create a video app with JaaS, React and Vercel</title>
      <dc:creator>Chris Corcoran</dc:creator>
      <pubDate>Wed, 14 Jul 2021 19:10:14 +0000</pubDate>
      <link>https://dev.to/chriscorcoran/create-a-video-app-with-jaas-react-and-vercel-49fe</link>
      <guid>https://dev.to/chriscorcoran/create-a-video-app-with-jaas-react-and-vercel-49fe</guid>
      <description>&lt;h2&gt;
  
  
  👋🏼 Introduction
&lt;/h2&gt;

&lt;p&gt;Over the past couple of months, I've had the opportunity to work with the &lt;a href="https://jitsi.org/" rel="noopener noreferrer"&gt;Jitsi&lt;/a&gt; team at &lt;a href="https://www.8x8.com/" rel="noopener noreferrer"&gt;8x8&lt;/a&gt; on improving the developer experience of their &lt;a href="https://jaas.8x8.vc" rel="noopener noreferrer"&gt;Jitsi-as-a-Service (JaaS)&lt;/a&gt; product. One of the most enjoyable parts of my work was building sample apps using the JaaS APIs. I decided to turn one of these apps into a tutorial to share what I've learned and to show off JaaS :)&lt;/p&gt;

&lt;p&gt;This tutorial will show you how easy it is to get up and running with &lt;a href="https://jaas.8x8.vc" rel="noopener noreferrer"&gt;Jitsi-as-a-Service (JaaS)&lt;/a&gt;. This tutorial will build a serverless video meeting application using React, Vercel, and JaaS. Our simple application will provide users with a form to enter their email. That form will request a serverless function to see if the user is allowed to join. If the user can join, the function will generate a &lt;a href="https://jwt.io/" rel="noopener noreferrer"&gt;JSON Web Token (JWT)&lt;/a&gt;, and the frontend will add the user to the video call.&lt;/p&gt;

&lt;p&gt;For the complete code, see this &lt;a href="https://github.com/chriscorcoran/jaas-hands" rel="noopener noreferrer"&gt;GitHub repo&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  📚 Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before we get started, there are a few prerequisites we need to sort out.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;JaaS Account&lt;/strong&gt; - If you don’t already have a JaaS account, you can create one for free by going to &lt;a href="https://jaas.8x8.vc" rel="noopener noreferrer"&gt;https://jaas.8x8.vc&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Node.js&lt;/strong&gt; - If you’re not familiar with node and want to learn more, check out the Introduction to Node.js guide&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vercel Account&lt;/strong&gt; - You can create a free account by going to &lt;a href="https://vercel.com/#get-started" rel="noopener noreferrer"&gt;https://vercel.com/#get-started&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Any code editor of your choice&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  👷‍♀️ Building a React App
&lt;/h2&gt;

&lt;p&gt;We’re going to start by using the Create React App utility to initialize a new React App. You’ll need to have Node.js installed on your development machine if you don’t already.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;After running &lt;code&gt;npm start,&lt;/code&gt; you should load the template application by going to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cleaning Up Scaffolding
&lt;/h3&gt;

&lt;p&gt;The Create React App utility creates some scaffolding that we won’t be using. To simplify our project, we can just remove some of that scaffolding now. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We also need to clean up a few references to these files in &lt;code&gt;App.js&lt;/code&gt; and &lt;code&gt;index.js&lt;/code&gt;. In &lt;code&gt;index.js&lt;/code&gt;, remove the following line: &lt;code&gt;import './index.css';&lt;/code&gt; In  App.js, be sure to remove the line &lt;code&gt;import './App.css';&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing Dependencies
&lt;/h3&gt;

&lt;p&gt;For our project, we’re going to need a few different libraries. We will need bootstrap for styling, UUID for generating user IDs, and jsonwebtoken to generate JSON Web Tokens (JWTs). To install these dependencies, simply run this command from inside your project directory.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Once you’ve installed the dependencies, we need to make a small change to &lt;code&gt;index.js&lt;/code&gt; to load bootstrap. Simply add this line to your existing import statements.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Creating the UI
&lt;/h3&gt;

&lt;p&gt;Now we’re ready to start building out our UI. We’re going to keep things simple for this application. We’ll present users with a form to enter their email addresses. We’ll use the provided email address to make a request to a serverless function which will determine if the user can join the meeting. If they can, then the serverless function will return a JWT, and we’ll load the JaaS embed.&lt;/p&gt;

&lt;h4&gt;
  
  
  Creating the Layout
&lt;/h4&gt;

&lt;p&gt;The first thing we’ll do is create the layout of our application. It will be a single React component in the &lt;code&gt;App.js&lt;/code&gt; file. Our layout will have two parts: an email address form and a video call. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  Managing State
&lt;/h4&gt;

&lt;p&gt;In our layout, we have a couple of essential pieces of state that we need to manage. We rely on a variable called ‘allowed’ to control which part of our layout to display. We also need to store the value of the email field to send to our serverless function. &lt;/p&gt;

&lt;p&gt;To do this, we’ll be using React Hooks. We just need to add a couple of lines to our &lt;code&gt;App&lt;/code&gt; component. You’ll want to insert these lines inside the &lt;code&gt;App&lt;/code&gt; definition before the return statement.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  🏗 Setting Up Vercel
&lt;/h2&gt;

&lt;p&gt;We’ll be using Vercel as our serverless function environment. Vercel makes it easy to develop a serverless function locally and then seamlessly deploy it to the cloud. To get started, we’ll first need to install the Vercel CLI. If you haven’t already. You can install the Vercel CLI by running the following command:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Once you have the CLI installed, we just need to initialize our project to run with Vercel by running the following command at the root of our project.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;With our project initialized now, we’re ready to have Vercel run our project locally. First, make sure to close any other instance of the React dev server; once you’ve done that, you can run the following command.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Just like running &lt;code&gt;npm start&lt;/code&gt;, the Vercel CLI will start a development server that is available by going to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  👩‍💻 Creating a Serverless Function
&lt;/h2&gt;

&lt;p&gt;Creating a serverless function for Vercel to run is easy. First, we need to create a new directory at the root of our project. Vercel relies on convention to discover your serverless functions. So, it’s essential you make this in the root directory of your project and not &lt;code&gt;./src&lt;/code&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Vercel will treat any source file you create in &lt;code&gt;./api&lt;/code&gt; as a serverless function. For our project, we want to create a new file called &lt;code&gt;join.js&lt;/code&gt;. This will create a new API endpoint at &lt;code&gt;/api/join&lt;/code&gt;. We’ll use this endpoint to determine if the user can join the meeting and generate the required JWT.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Our serverless function will be relatively straightforward. It just needs to parse the request from the front end, check if the provided email address is allowed to join the meeting, and then generate a JWT. Let’s start with the request handler that will orchestrate this process and generate our response. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you’ve probably noticed, the request handler is relatively simple and relies on few other functions to check for authorization and generate a JWT. Let’s first start with &lt;code&gt;isAllowed()&lt;/code&gt;. This function consults a comma delineated list of email addresses to determine if the user can join the meeting. To make it easy to update, we’re storing the list of email addresses in an environment variable.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;If the user is allowed to join the meeting, we need to generate a JWT that will enable them to enter. For that, we’re using the &lt;code&gt;generateJWT()&lt;/code&gt; function. This does the bulk of the work in our serverless function. The &lt;code&gt;generateJWT(0&lt;/code&gt; will create the payload of our JWT, decode our public key, and finally sign the JWT.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Inside of &lt;code&gt;generateJWT()&lt;/code&gt;, we’re calling out to yet another helper function to generate avatars for our users automatically. The &lt;code&gt;generateAvatar()&lt;/code&gt; function generates a Gravatar URL from the provided email address to populate avatars automatically.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;When we put it all together, our &lt;code&gt;join.js&lt;/code&gt; should look something like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  🎷 Adding a little JaaS
&lt;/h2&gt;

&lt;p&gt;Now that we have our React frontend and serverless function up and running, we need to integrate with JaaS. To do this, we need to configure our JaaS account, populate our environment variables and then make a few changes to our application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuring Environment Variables
&lt;/h3&gt;

&lt;p&gt;For our application to work, we’ll need to create a couple of different environment variables. You can do this by creating a &lt;code&gt;.env&lt;/code&gt; file at the root of your project. Inside the &lt;code&gt;.env&lt;/code&gt; file create the following variables. You’ll be able to access them from inside your application by using the &lt;code&gt;process.ENV.{Variable Name}&lt;/code&gt; variables. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can populate &lt;code&gt;ALLOW_LIST&lt;/code&gt; with a comma delineated list of email addresses that you want to allow access to the meeting. For example:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can also select &lt;code&gt;JAAS_ROOM_NAME&lt;/code&gt; that fits your use case. For example:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;For the remaining values, we’ll be consulting the JaaS web console to get the required values.&lt;/p&gt;

&lt;h3&gt;
  
  
  Gathering the JaaS Configuration
&lt;/h3&gt;

&lt;h4&gt;
  
  
  JaaS App ID
&lt;/h4&gt;

&lt;p&gt;Start by heading over to the API Keys section of the JaaS console. The first bit of information we’ll need to make a note of is your AppID. You can store that in the &lt;code&gt;JAAS_APP_ID&lt;/code&gt; environment variables.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fchriscorcoran.github.io%2Fjaas-hands-assets%2FJaaS%2520App%2520ID.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fchriscorcoran.github.io%2Fjaas-hands-assets%2FJaaS%2520App%2520ID.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  JaaS API Key
&lt;/h4&gt;

&lt;p&gt;Next, you’ll want to create a new API Key by clicking the 'Add API Key' button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fchriscorcoran.github.io%2Fjaas-hands-assets%2FJaaS%2520Add%2520API%2520Key.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fchriscorcoran.github.io%2Fjaas-hands-assets%2FJaaS%2520Add%2520API%2520Key.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will then be presented with a dialogue that asks if you want to generate a new key pair or add your own. For this tutorial, we want to generate a new key pair.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fchriscorcoran.github.io%2Fjaas-hands-assets%2FJaaS%2520Generate%2520API%2520Key%2520Pair.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fchriscorcoran.github.io%2Fjaas-hands-assets%2FJaaS%2520Generate%2520API%2520Key%2520Pair.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the key pair has been generated, you’ll be presented with another dialogue that asks if you want to download the new key pair. For our purposes, we’ll need to download the Private Key.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fchriscorcoran.github.io%2Fjaas-hands-assets%2FJaaS%2520Download%2520Key%2520Pair.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fchriscorcoran.github.io%2Fjaas-hands-assets%2FJaaS%2520Download%2520Key%2520Pair.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you’ve downloaded the public key, we need to base64 encode it to store it in the &lt;code&gt;JAAS_PRIVATE_KEY&lt;/code&gt; environment variable. To do this, run the following command:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Tying It All Together
&lt;/h3&gt;

&lt;p&gt;Now that we have a working frontend and serverless function, it’s time to tie it all together. To that, we’ll need to update our React front end to talk to our serverless function. We’ll do this by updating our App component to catch the &lt;code&gt;onSubmit&lt;/code&gt; and send a request to our serverless function. &lt;/p&gt;

&lt;p&gt;The first thing we need to do is load the JaaS iFrame API into our React app. To do this, we’ll turn to our good friend React Hooks. We should make sure to group this new hook with our other state management hooks.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Next, we’ll need a function to make a request to our serverless function for the authorization check and JWT generation. To do that, we’ll create a function to catch the submit function on our form that looks like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now we just need to connect this new onSubmit function to our form. To do that, we just add the onSubmit event handler to our layout, like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Finally, we just need to make our call to initialize the video meeting. For that, we’ll add one last function called &lt;code&gt;initJaas()&lt;/code&gt;. When our request to the serverless function is successful, the front end will call &lt;code&gt;initJaas()&lt;/code&gt; to start the call.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  🚀 Congratulations!
&lt;/h2&gt;

&lt;p&gt;You did it! You’ve successfully set up a serverless video meeting application using React, Vercel, and JaaS. Now it’s time to ship it! When you’re ready to take it to the next level, run the following command to deploy to the cloud! &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


</description>
      <category>react</category>
    </item>
  </channel>
</rss>
