feature image voice vue express

Creating a Voice Chat Application with Vue.js and Express

Published August 05, 2019 by Garann Means

Adding a service to a complex web application can be tricky to do in a maintainable way. This is even more true when the service has a user interface component. With Nexmo’s API you can create an in-browser voice chat that becomes the basis for a variety of communications applications. But even organizing the pieces of that basic UI can be difficult. Components like those used in Vue.js make this easier by providing a pattern for the templates, styling, and UI scripting an individual UI component may require. An Express server connecting to Nexmo’s tools gives you a lightweight full-stack solution that can be adapted to whatever real-world architecture you end up with, thanks to separation of concerns.

There are many ways you can structure an application with Vue. For this tutorial I’ll remix a Glitch project that provides relatively little scaffolding, but you could choose a starter project provided by the Vue CLI or a third-party library offering specific features like Server-Side Rendering. Because your code will rely on both Vue and Express, the only requirement is that your setup includes both.

Adding Nexmo To Your Project

To create a conversation from the browser you’ll need to install both the Nexmo client and server packages. Because the user will send data from the client you’ll also need body-parser to use in Express. In your project’s root directory, install those packages with npm, or from the console in Glitch use pnpm:

To use Nexmo’s tools, you’ll also need to provide your API credentials in the .env file. The file should look something like this:

Depending on your environment, you may also need to install the dotenv package from npm. To import your environment variables from .env, you just need to add a single line at the top of your server.js file: require('dotenv').config();

You can find your API key and secret on the Getting Started page of your Nexmo dashboard. Under the Voice menu, go to Create an Application and click “Generate public/private key pair” to download your private.key file. Then fill in the fields and click “Create application” to get your Application ID.

Be sure to copy your private.key file into your project and update the path in .env to the location you’ve saved it to. It’s possible to paste the contents directly into .env, but the formatting may cause issues. It’s generally more robust to keep it in a separate file.

A Server For API Calls

The role of Express.js in your project will be to provide a simple server that calls the Nexmo API to perform a few admin tasks. This will require some setup of the server itself, a Nexmo instance, and route definitions for your server endpoints.

In server.js, create the server and direct it to parse JSON in request bodies and serve static pages from the public directory. Next, create a Nexmo object, passing it the values from .env. Finally, create placeholders for your routes and tell the server to begin listening for events:

Server Routes

The three routes defined on the server will allow the application to list and create users who can join a conversation, and to authenticate them. In an application for real world use, you’d probably connect this to your own user management instead of a web interface.

The route /getJWT provides a token the client can use to authenticate the current user. Producing the JWT is done with a single function, but it requires several pieces of data. You need to supply your application ID again, as well as sub, which is the username you want to authenticate. You’ll also set the expiration and allowed paths for the token. You can send the newly created token on to the client:

The /getUsers path also makes a single call and returns its result, but let’s tidy it up a bit for use in a web interface. Before returning the list of all users in this application, you can filter out system users whose IDs begin with the prefix NAM-. In a real world application where user IDs were mapped to accounts within your larger app, you probably wouldn’t bother with this step and could return the list as-is:

The last route, /createUser, will take some user input and add a user to the application. Because the create function takes both a user name and a display name as input there’s the option in this code to set a separate display name, however we won’t include that in the UI. Therefore, the endpoint only looks for a name from the client, and once it creates a user with it, returns their ID:

The Vue App Component

All the Vue components for this project will live in the src directory. The project I’m remixing already includes a main.js file there that creates a Vue instance, as well as a container component in app.vue. main.js does nothing more than render the App component:

This works in concert with public/index.html, where a div with the ID app is the only element on the page:

In case we want to add more to it later, we’ll leave the App component in place and load a Nexmo component within it, rather than replace App with Nexmo. If you already have an app.vue file, you can replace its contents with a simple template and script that just loads the Nexmo component:

The Nexmo Component

The Nexmo component is where things begin to get interesting. You can create it at nexmo.vue and add a template at the start of the file that will render User and Conversation components. For User, an update hook will call a function getJWT in the script you’ll add next. You can also add a reference to the component to access it later on:

Below the template you’ll add a script tag containing the component’s logic. After importing the two subcomponents and the Nexmo Client SDK, you’ll export a Vue component named Nexmo. It will contain some empty data properties that will be part of its state, as well as its subcomponents and some methods you’ll define next:

The methods property will define two functions, one to get a JWT from the server and one to handle logging in. The getJWT function is called by the update hook on your User component, so it should first check whether that component contains a username property. If it does, it can call the server-side /getJWT endpoint using fetch. It passes the stringified username value, and if everything works smoothly, gets a JWT in return. It stores the JWT as a property of the instance and calls the login function.

The login function is where you’ll instantiate an actual Nexmo client. You’ll log in your user with their JWT, then set a flag if that succeeds and save a reference to the Nexmo application. Once you have the application you can get the conversations the current user is invited to:

The User Component

The User component at src/user.vue is the first place you’ll have a template that does something besides render a subcomponent. This is another bit you could skip in a production app, but in this case the user login interface is part of your Nexmo app. The template will show the connected user if one exists. If not, it shows a form with two paths. The first lets someone select an existing user from a dropdown. If a selection is made, the user is immediately updated by the setExistingUser function.

A user can also supply a new username and click the submit button. This calls the createUser function:

User Component Script

The script for the component has a few different things going on, but no complex logic. Most of what it’s doing is loading and saving properties. The complex stuff happens within the Vue framework itself, in functionality like the update hook in your Nexmo component.

There’s nothing to import, so you can immediately export a User component. The only data it will need are properties for the user’s ID and name, and a list of current users in the application.

The component has four methods to support the form in the template. The getUsers function calls /getUsers on the server to fetch the list of users. You’ll remember that you’ve handled any necessary filtering logic server-side, so if there’s no error you can just set that property on the component.

setExistingUser is called by an onchange event on the users dropdown. It saves the username and user ID of the selection made. For new users, setUsername is also called by an onchange, this time on the text field. Updating the new username on the component each time it changes saves you having to get a reference to the text field element. If a user clicks the “Create user” button, createUser is called, sending the username in state to the server and saving the user ID that gets returned.

After methods, this component also calls beforeMount to make sure the list of users gets loaded when it’s first initialized:

The Conversation Component

Up to now, the code you’ve written has been to create your Nexmo app, set a user, and log in. It should be just separate enough that you can make changes to support the needs of your individual project while still exposing the essential pieces of information to your larger Vue app. Now you can use those pieces to join a conversation. The conversation is the jumping off point for a variety of types of communication you might wish to do using Nexmo’s API on the client.

The component would be more manageable with a few subcomponents split out (e.g. the audio controls). However, to make things more obvious for now you can put all the code together in conversation.vue.

The Template

At the top level of the template is a conditional to determine whether an ongoing conversation exists. If it does, you’ll render an audio element to supply the sound, and two buttons to enable and disable audio. When clicked they’ll call enableAudio and disableAudio, respectively.

If there’s no current conversation the user will need to join or start one. If the user has been invited to a conversation or has previously started one, it will appear in the invites array on the parent Nexmo component. The values in invites will populate a dropdown of conversations, and selecting one will call the joinConversation function. Whether or not the user has existing invites, they’ll see a button to start a new conversation:

The Script

Again in this component, the only thing happening at the top level of the script tag is exporting a Conversation component. Its only data are the current conversation and a flag keeping track of whether audio is enabled.

The methods the component contains are all quite straightforward. createConversation calls the newConversation function of the app stored on the parent Nexmo component, then stores the created conversation. joinConversation does the same thing except that it calls the app’s getConversation function, passing it the ID of the conversation selected in the dropdown.

In enableAudio, you first need to enable media on the current conversation. That will give you a stream which you can set as the srcObject or src of the audio element in your template. Once the metadata has loaded, you can play the stream and set the component’s audioOn flag to true. The disableAudio function called when the “Disable audio” button is clicked is simpler. It just needs to disable media in current_conv and then set the audioOn flag back to false:

The Rest

There are a few things we haven’t covered, which are hopefully supplied by the Vue boilerplate you’ve chosen or don’t necessarily have to affect the logic of your application. For example, in my own code I’m relying on Browserify and Vueify, as well as a bit of CSS that was part of the project I remixed. The build step that makes the Vue side of the application work is defined in “scripts” in package.json:

Next Steps

The code you’ve written is very much a starting point for your real-world work. As mentioned, you’ll probably want to replace the test user management system with something that ties conversation members to your own authenticated users. With your conversation created you can send and receive messages, receive calls, and set up audio conferencing.

Read more about the Nexmo Client SDK to find out what you can do next:

Leave a Reply

Your email address will not be published.