Learn how to send Deepgram a Finalize message, which flushes the websocket stream’s audio by forcing the server to process all unprocessed audio data immediately and return the results.

Streaming

In real-time audio processing, there are scenarios where you may need to force the server to process (or flush) all unprocessed audio data immediately. Deepgram supports a Finalize message to handle such situations, ensuring that interim results are treated as final.

What is the Finalize Message?

The Finalize message is a JSON command that you send to the Deepgram server, instructing it to process and finalize all remaining audio data immediately. This is particularly useful in scenarios where an utterance has ended, or when transitioning to a keep-alive period to ensure that no previous transcripts reappear unexpectedly.

Sending Finalize

To send the Finalize message, you need to send the following JSON message to the server:

JSON
1{
2 "type": "Finalize"
3}

You can optionally specify a channel field to finalize a specific channel. If the channel field is omitted, all channels in the audio will be finalized. Note that channel indexing starts at 0, so to finalize only the first channel you need to send:

JSON
1{
2 "type": "Finalize",
3 "channel": 0
4}

Finalize Confirmation

Upon receiving the Finalize message, the server will process all remaining audio data and return the final results. You may receive a response with the from_finalize attribute set to true, indicating that the finalization process is complete. This response typically occurs when there is a noticeable amount of audio buffered in the server.

If you specified a channel to be finalized, use the response’s channel_index field to check which channel was finalized.

JSON
1{
2 "from_finalize": true
3}

In most cases, you will receive this response, but it is not guaranteed if there is no significant amount of audio data to process.

Language-Specific Implementations

Following are code examples to help you get started.

Sending a Finalize message in JSON Format

These snippets demonstrate how to construct a JSON message containing the “Finalize” type and send it over the WebSocket connection in each respective language.

1const WebSocket = require("ws");
2
3// Assuming 'headers' is already defined for authorization
4const ws = new WebSocket("wss://api.deepgram.com/v1/listen", { headers });
5
6ws.on('open', function open() {
7 // Construct Finalize message
8 const finalizeMsg = JSON.stringify({ type: "Finalize" });
9
10 // Send Finalize message
11 ws.send(finalizeMsg);
12});

Streaming Examples

Here are more complete examples that make a streaming request and use Finalize. Try running these examples to see how Finalize can be sent to Deepgram, forcing the API to process all unprocessed audio data and immediately return the results.

1const WebSocket = require("ws");
2const axios = require("axios");
3const { PassThrough } = require("stream");
4
5const apiKey = "YOUR_DEEPGRAM_API_KEY";
6const headers = {
7 Authorization: `Token ${apiKey}`,
8};
9
10// Initialize WebSocket connection
11const ws = new WebSocket("wss://api.deepgram.com/v1/listen", { headers });
12
13ws.on("open", async function open() {
14 console.log("WebSocket connection established.");
15
16 try {
17 // Fetch the audio stream from the remote URL
18 const response = await axios({
19 method: "get",
20 url: "http://stream.live.vc.bbcmedia.co.uk/bbc_world_service",
21 responseType: "stream",
22 });
23
24 const passThrough = new PassThrough();
25 response.data.pipe(passThrough);
26
27 passThrough.on("data", (chunk) => {
28 ws.send(chunk);
29 });
30
31 passThrough.on("end", () => {
32 console.log("Audio stream ended.");
33 finalizeWebSocket();
34 });
35
36 passThrough.on("error", (err) => {
37 console.error("Stream error:", err.message);
38 });
39
40 // Send Finalize message after 10 seconds
41 setTimeout(() => {
42 finalizeWebSocket();
43 }, 10000);
44 } catch (error) {
45 console.error("Error fetching audio stream:", error.message);
46 }
47});
48
49// Handle WebSocket message event
50ws.on("message", function incoming(data) {
51 let response = JSON.parse(data);
52 if (response.type === "Results") {
53 console.log("Transcript: ", response.channel.alternatives[0].transcript);
54 }
55});
56
57// Handle WebSocket close event
58ws.on("close", function close() {
59 console.log("WebSocket connection closed.");
60});
61
62// Handle WebSocket error event
63ws.on("error", function error(err) {
64 console.error("WebSocket error:", err.message);
65});
66
67// Send Finalize message to WebSocket
68function finalizeWebSocket() {
69 const finalizeMsg = JSON.stringify({ type: "Finalize" });
70 ws.send(finalizeMsg);
71 console.log("Finalize message sent.");
72}
73
74// Gracefully close the WebSocket connection when done
75function closeWebSocket() {
76 const closeMsg = JSON.stringify({ type: "CloseStream" });
77 ws.send(closeMsg);
78 ws.close();
79}
80
81// Close WebSocket when process is terminated
82process.on("SIGINT", () => {
83 closeWebSocket();
84 process.exit();
85});

Conclusion

Using the Finalize message with Deepgram’s API allows for precise control over the finalization of audio processing. This feature is essential for scenarios requiring immediate processing of the remaining audio data, ensuring accurate and timely results.


Built with