Audio Keep Alive
Learn how to send messages while streaming audio, ensuring uninterrupted communication.
KeepAlive
serves as a crucial mechanism for maintaining an uninterrupted connection with Deepgram’s servers, allowing you to optimize your audio streaming experience while minimizing costs.
What is theKeepAlive
message?
A common situation is needing to keep a connection open without constantly sending audio. Normally, you’d have to send data all the time, even silence, which wastes resources and increases costs since Deepgram charges for all audio, whether it’s speech or silence. KeepAlive
solves this by allowing you to pause the connection and resume later, avoiding extra costs for transcribing silence.
Benefits
- Cost Efficiency:
KeepAlive
enables you to optimize costs by pausing the connection during periods of silence, eliminating the need to transcribe unnecessary audio data. - Connection Maintenance: By sending periodic
KeepAlive
messages, you can ensure that the WebSocket connection remains open, preventing timeouts and maintaining communication with Deepgram’s servers. - Flexibility:
KeepAlive
offers flexibility in managing your streaming sessions. You can temporarily halt the transmission of audio data while keeping the connection active, resuming data streaming when needed without re-establishing the connection.
Sending KeepAlive
To send the KeepAlive
message, you need to send the following JSON message to the server:
Because Deepgram’s streaming connection is set to time out after 10 seconds of inactivity, it’s essential to periodically send KeepAlive
messages to maintain the connection and prevent it from closing prematurely.
If no audio data is sent within a 10-second window, the connection will close, triggering a NET-0001
error. Using KeepAlive
extends the connection by another 10 seconds. To avoid this error and keep the connection open, continue sending KeepAlive
messages 3-5 seconds before the 10 second timeout window expires until you are ready to resume sending audio.
Be sure to send the KeepAlive message as a text WebSocket message. Sending it as a binary message may result in incorrect handling and could lead to connection issues.
KeepAlive
Confirmation
You will not receive a response back from the server.
Language Specific Implementations
Following are code examples to help you get started.
Sending a KeepAlive
message in JSON Format
These snippets demonstrate how to construct a JSON message containing the KeepAlive
type and send it over the WebSocket connection in each respective language.
Streaming Examples
Here are more complete examples that make a streaming request and use KeepAlive
. Try running these examples to see how KeepAlive
is sent periodically.
Deepgram SDKs
Deepgram’s SDKs make it easier to build with Deepgram in your preferred language. To learn more about getting started with the SDKs, visit Deepgram’s SDKs documentation.
Word Timings
Word timings for transcription results returned from a stream are based on the audio sent, not the lifetime of the websocket. If you send KeepAlive
messages without sending audio payloads for a period of time, then resume sending audio payloads, the timestamps for transcription results will pick up where they left off when you paused sending audio payloads.
Here is an example timeline demonstrating the behavior: