Article·Tutorials·Jun 20, 2022

How to Add Speech Recognition to Your React and Node.js Project

Bekah Hawrot Weigel
By Bekah Hawrot Weigel
PublishedJun 20, 2022
UpdatedJun 13, 2024

One of my favorite pieces of advice for learning a new technology is to build a project that solves a need or interests you. I’ve been interested in finding ways to improve mental health for a long time. If you have a React project you can follow along with this post to add Deepgram for speech-to-text transcription to your project. If you don't, I've got you covered with a React project called Affirmation, that uses automatic speech recognition to boost self-confidence.

Before you jump into the code, I want to share a little bit about the inspiration for the project. According to Christopher N Cascio, et al., “Self-affirmation activates brain systems associated with self-related processing and reward and is reinforced by future orientation.” Other studies have indicated that motivational self-affirmations can impact how you view youself and your performance; they also can be more effective if spoken aloud. You’ll be taking an existing React project with a complete front-end and adding the capability to speak and transcribe your affirmation.

Getting Started with React

Prerequisites

  • Understanding of JavaScript and React

  • Familiarity of hooks

  • Understanding of HTML and CSS

  • Node.js installed on your computer

If you want to follow along with this project, you can find the code for the front-end here. To get started quickly, I used Create React App. The file structure for this project will be similar to what you get with Create React App, but you’ll notice that you have a component called Affirmation.js.

Once you’ve forked or cloned the code, cd into the app.

In your terminal run npm install to install the dependencies you can find the package.json file. Then run npm run start and navigate to http://localhost:3000/. You should see your app up and running. Right now, everything you see is being rendered from the App.js file. Here’s what you should see.

Adding Speech-to-Text with Deepgram's Node SDK

Now that your project is up and running, you can get started with adding the speaking capabilities with our Automatic Speech Recognition (ASR) technology. You’ll add a new button that allows the user to give microphone access and share their affirmation aloud.

When they do this, the audio will be processed using Deepgram’s Node SDK, and the transcription will be submitted and appear on the screen. Although you could go deeper with this project by allowing the user to save the affirmation or collect all the affirmations, for the scope of this project, you’ll be showing one transcript at a time.

Updating Your Front-End

Before you add your backend, update your Affirmations.js file. Below your Submit button, add a Voice button with the following code:

You’ll notice that you have an onClick function called activateMicrophone, which doesn’t exist yet. So next, create that function.

Just below your handleChange function, add the function with a console.log and the steps you need to take to get things working.

To add microphone access, you’ll use the Media Streams API. Setting this up allows the browser to ask the user for access to their microphone. You do this by using the MediaDevices interface. Designate that you’re using audio and then create a new variable const mediaRecorder to use when implementing Deepgram.

Below the "Add microphone access" comment, add the following:

It's time to pause. You've made it as far as you can without connecting to the server.

Creating a Server Connection

Now you’re going to work on setting up your connection to Deepgram’s Node.js SDK and WebSocket connection.

Because you’re using API keys, you want to keep them safe. To learn more about keeping your API keys safe, check out Kevin’s post Browser Live Transcription - Protecting Your API Key. Using the terminal, let’s run npm i @deepgram/sdk dotenv to add Deepgram and dotenv to your project.

Next, you’ll need to:

  • Create a Deepgram API Key with an admin or owner role - get it here.

  • Create a file called .env and add DG_KEY='your-API-key'.

At the root of your project, add a server folder with a server.js file. In that file, you need three things to happen:

  1. Create a WebSocket connection

  2. When the WebSocket connection is open, Deepgram will create a live transcription.

  3. Once the data is received, send the transcript (as data) to your Affirmation.js component to record in your app.

To do this, use the following code:

Your server is ready to go! Now you just need to put the finishing touches on your Affirmation.js file.

Connecting the WebSocket to the Front-end

You need to be able to check if the WebSocket is open. To do this, you’re going to use the built-in hook from React, useRef.

Make sure you import useRef. Once you’ve done that, add const socketRef = useRef(null) just below your finalAffirmation hook.

Now you’re ready to connect our frontend code to your server.

Within the activateMicrophone function-below the mediaRecorder variable-you’ll:

  • Create and open a new WebSocket.

  • Update the value of setAffirmation with the results of the transcript.

  • Close the socket and handle errors.

Go ahead and add this to your file:

You’re almost there. Your very last step is to close your WebSocket in your handleSubmit function if it’s open. Just before setFinalAffirmation(true) add the following:

Go ahead and run this now. You should still have your React app running on localhost:3000, but you need to get that server running. To do that, go to your terminal and run node server/server.js. Click the Voice button.

You should get a pop-up asking you to allow the use of your microphone. Go ahead and give your browser permission. Now, test it out. Try using this affirmation: “I am intelligent.”

You should see that text in your text box. Hit submit. There it is!

As you’ve seen, there are a couple of steps involved to get Deepgram live transcription in your React project, but luckily, the process is very repeatable once you’ve done it. And now you’ve done it! You can also find all the code in the repo for this project. To learn more about the features you have access to with our Node SDK, check out our Node SDK documentation.

Learn more about Deepgram

Sign up for a Deepgram account and get $200 in Free Credit (up to 45,000 minutes), absolutely free. No credit card needed!

We encourage you to explore Deepgram by checking out the following resources:

  1. Deepgram API Playground 

  2. Deepgram Documentation

  3. Deepgram Starter Apps

Unlock language AI at scale with an API call.

Get conversational intelligence with transcription and understanding on the world's best speech AI platform.