Live Streaming Audio Transcription
An overview of the Deepgram JavaScript SDK and Deepgram speech-to-text live streaming.
The Deepgram JavaScript SDK now works in both server and browser environments.
Installing the SDK
# Install the Deepgram JS SDK
# https://github.com/deepgram/deepgram-js-sdk
npm install @deepgram/sdk
Initialising the SDK
import { createClient } from "@deepgram/sdk";
const deepgram = createClient("DEEPGRAM_API_KEY");
Connecting to Deepgram
Connecting to Deepgram returns an instance of LiveClient
.
const live = deepgram.listen.live({ model: "nova" });
Live Transcription Options
Transcription options are provided as an object to the live(...options)
method. Each of these parameters are found as unique features on the Streaming API reference.
Events
Event's are available from the SDK as an enum.
import { LiveTranscriptionEvents } from "@deepgram/sdk";
Events sent by the SDK
The following events are returned by the SDK.
Event | Description | Data |
---|---|---|
LiveTranscriptionEvents.Open | The web socket connection to Deepgram has been opened. | Instance of LiveClient |
LiveTranscriptionEvents.Close | The web socket connection to Deepgram has been closed. | WebSocket.CloseEvent |
LiveTranscriptionEvents.Transcript | Deepgram has responded with a transcription result. | Results |
LiveTranscriptionEvents.Metadata | Deepgram has responded with metadata. | Metadata |
LiveTranscriptionEvents.Error | An error occurred. | Error |
LiveTranscriptionEvents.Warning | The SDK has issued a warning to do with your request/usage. | Warning |
Listening to Events
The events occur as native JS EventListener
events. Please note, .on()
is an alias of addEventListener on an EventEmitter
.
live.on(LiveTranscriptionEvents.Open, () => {
live.on(LiveTranscriptionEvents.Transcript, (data) => {
console.log(data);
});
});
Functions
An instance of LiveClient
has the following functionality.
Sending Data
You can send raw audio data or text to the web socket without a content type. We will determine the appropriate encoding from the data.
live.send(data);
Keep Alive
You can send Deepgram a keep alive if you intend for deliberate moments of non-billable silence while the socket remains open. The connection will close after 12 seconds if no data is sent, but you will still be billed for an active connection if you're sending data, even if no audible words are included in the data.
live.keepAlive()
Read more about KeepAlive in this comprehensive guide.
Get Ready State
The getReadyState
function returns the ready state of the web socket connection to Deepgram.
live.getReadyState();
Toggle Features
The configure
function gives the ability to toggle different features. Currently, only the numerals
feature is supported. This function is a shorthand way of sending the following data to the web socket connection:
live.configure({numerals: true}); // enable numerals
Closing the Connection
The requestClose
function closes the web socket connection to Deepgram.
live.requestClose();
Updated about 2 months ago