Close Stream

Send a CloseStream message to close the WebSocket stream.
Streaming:Nova

Use the CloseStream message to close the WebSocket stream. This forces the server to immediately process any unprocessed audio data and return the final transcription results.

Purpose

In real-time audio processing, there are scenarios where you may need to force the server to close. Deepgram supports a CloseStream message to handle such situations. This message will send a shutdown command to the server instructing it to finish processing any cached data, send the response to the client, send a summary metadata object, and then terminate the WebSocket connection.

Example Payloads

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

JSON
{
"type": "CloseStream"
}

Upon receiving the CloseStream message, the server will process all remaining audio data and return the following:

JSON
{
"type": "Metadata",
"transaction_key": "deprecated",
"request_id": "8c8ebea9-dbec-45fa-a035-e4632cb05b5f",
"sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"created": "2024-08-29T22:37:55.202Z",
"duration": 0.0,
"channels": 0
}

Language-Specific Implementations

Below are code examples to help you get started using CloseStream.

const WebSocket = require("ws");
// Assuming 'headers' is already defined for authorization
const ws = new WebSocket("wss://api.deepgram.com/v1/listen", { headers });
ws.on('open', function open() {
// Construct CloseStream message
const closeStreamMsg = JSON.stringify({ type: "CloseStream" });
// Send CloseStream message
ws.send(closeStreamMsg);
});