Integrating Speech-to-Text API in Next.js: Real-Time Audio Apps

Learn how to integrate a Speech-to-Text API in Next.js to build real-time audio apps. This guide covers WebSocket streaming, client-side audio, and low-latency transcription.

In this article
  1. Why Next.js Is Ideal for Speech Applications
  2. Preparing the System Architecture
  3. Data Processing Flow
  4. Setting Up the WebSocket Connection
  5. Implementing Client-Side Recording
  6. Handling Results and Optimizing User Experience
  7. Visual Text Display
  8. Handling Vietnamese and Code-Switching
  9. Comparison of Integration Methods
  10. Expert Advice
  11. Summary and Next Steps

Integrating speech recognition into modern web applications is no longer a complex technical hurdle, especially when you master real-time data flows. This guide explains how to build a real-time recording application using a Speech-to-Text (STT) API in Next.js. You will learn to convert speech to text instantly with minimal latency, covering core technical steps from WebSocket setup to client-side audio processing for a smooth, professional user experience.

Why Next.js Is Ideal for Speech Applications

Next.js offers strong support for real-time interactive applications through efficient state management and server-side rendering capabilities. When paired with modern STT services, it allows you to build features like meeting recording, voice input, or virtual assistants directly in the browser without complex software installations.

The key to "real-time recording" is streaming. Instead of waiting to record an entire file before sending it for processing (batch processing), streaming allows you to send small audio chunks to the server as the user speaks. This ensures text results appear on the screen almost immediately, creating a natural interactive feel.

Preparing the System Architecture

To deploy successfully, you must understand the data flow between the browser, the Next.js server, and the STT service.

Data Processing Flow

  1. Client (Browser): Uses the MediaRecorder API or Web Audio API to capture audio. The audio is divided into small packets (typically 16kHz PCM or Opus).
  2. Server (Next.js API Route): Acts as an intermediary. It receives audio data from the client and forwards it to the STT service via a WebSocket connection. This protects your API key and handles business logic.
  3. STT Service: Receives the audio stream, processes it with speech recognition models, and sends text results (partial results) back via WebSocket.

Setting Up the WebSocket Connection

In Next.js, you can create a dedicated API Route to handle WebSockets. Here is the basic code structure:

// app/api/stt/stream/route.js
import { WebSocket } from 'ws';

export async function GET(request, { params }) {
  const { searchParams } = new URL(request.url);
  const apiKey = searchParams.get('key'); // Get from env or authenticate

  // Initialize WebSocket connection to STT service
  // Handle message events from client and service
  // Send text results back to client
}

Important note: Ensure you handle open, message, close, and error events carefully to avoid memory leaks or hung connections.

Implementing Client-Side Recording

Browser-side audio capture must be optimized to reduce latency. Follow these specific steps:

  • Check Permissions: Call navigator.mediaDevices.getUserMedia({ audio: true }) to request microphone access.
  • Create MediaStream: Capture audio into a MediaStream.
  • Convert Format: Use AudioContext and ScriptProcessorNode (or AudioWorklet for higher performance) to convert binary audio into a format suitable for the STT API (e.g., PCM 16-bit, 16kHz).
  • Send Data: Use WebSocket to send audio blocks to the Next.js server.

Practical tip: Send audio packets continuously every 100–200ms. If you send too slowly, text latency increases; if too quickly, the server may become overloaded.

Handling Results and Optimizing User Experience

When receiving results from the STT API, you will have two types of data: partial (temporary results that change continuously) and final (the final result of a sentence or segment).

Visual Text Display

  • Partial Results: Display temporary text with a faded color or italics to indicate it is not yet official.
  • Final Results: When a final result is received, "lock" this text segment and add it to the complete transcript.

Handling Vietnamese and Code-Switching

A significant challenge for Vietnamese STT is code-switching (mixing English). For example: "Send the email to the team now." Specialized Vietnamese STT models, such as those from AIVISION, are trained on large datasets to handle these cases effectively, resulting in more natural and accurate output.

Comparison of Integration Methods

Criterion Batch Processing (File) Real-time Streaming (WebSocket)
Latency High (waits for file) Low (real-time)
Application Upload old audio files Live recording, meetings
Complexity Low High (requires stream handling)
Experience Waiting Instant interaction

Expert Advice

When building your application, do not forget to optimize for network conditions. If users have unstable connections, implement buffering and retry mechanisms. Additionally, clearly display connection status (recording, processing, finished) so users do not worry about data loss.

AIVISION provides high-accuracy STT APIs for Vietnamese, supporting both streaming via WebSocket and file transcription via REST. With over 690,000 hours of Vietnamese data used for training, AIVISION's models handle both standard read speech and natural speech in real-world environments, including medical and business terminology.

Summary and Next Steps

Integrating an STT API into Next.js to create real-time recording apps is a promising project that opens the door to direct voice interaction on the web. By using WebSocket and client-side audio streaming, you can create a smooth, low-latency, and accurate experience.

If you are looking for a reliable STT solution with deep Vietnamese support and easy integration, try the AIVISION platform. You can start immediately with a free account to test recognition quality and API response speed.

Contact the technical team if you need in-depth integration support. Start free to experience the Speech-to-Text and Text-to-Speech APIs immediately. See other technical articles at Blog.

Frequently asked questions

Does integrating an STT API in Next.js require complex libraries?

No, you only need standard Node.js libraries like `ws` for WebSocket and standard browser web APIs like `MediaRecorder` or `AudioContext`. Next.js supports this well through API Routes.

How can I reduce latency when recording in real-time?

The key is streaming. Send small audio chunks continuously via WebSocket instead of waiting for the entire file. Also, optimize the audio format (16kHz PCM) to reduce the data size transferred.

Does AIVISION support both Vietnamese and English?

Yes. AIVISION's STT models are designed to handle Vietnamese well and support code-switching (mixing English), helping the output text remain natural and accurate in real conversations.

Do I need to pay before trying the API?

No. AIVISION provides services completely free for every account, with no credit card required. You only need to sign up and use the service within the daily usage limit.

Can I deploy this recording application on mobile web?

Yes, web APIs like `getUserMedia` and `WebSocket` work well on modern mobile browsers. However, you should check compatibility with specific mobile browsers (Chrome, Safari, Firefox).

Try AIVISION's Vietnamese speech AI

Speech-to-Text, Text-to-Speech and LLM, free.

Start free → Contact

By

Dr. Giang Vo — CEO, AIVISION

Dr. Giang Vo is the CEO of AIVISION, leading the development of s2speech — speech recognition, AI voices and a large language model for Vietnamese — and working with companies at home and abroad on their deployments.

0981 419 967 · giang.vo@aivgroups.com

#Speech-to-Text API#Next.js#real-time audio#WebSocket#voice recognition#web development#AIVISION

Related articles