> ## Documentation Index
> Fetch the complete documentation index at: https://slng.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# HTTP vs. WebSocket protocols

> Compare HTTP and WebSocket protocols on SLNG. Latency, flow, complexity, and when to use each for text-to-speech and speech-to-text workloads.

Every TTS and STT model is reachable over both HTTP and WebSocket. The URL stays the same; only the protocol changes.

The snippets on this page use `SLNG_API_KEY` as a placeholder. Replace it with an SLNG key from [app.slng.ai/api-keys](https://app.slng.ai/api-keys) before running the code.

## At a Glance

| | HTTP | WebSocket |
| - | - | - |
| **Flow** | Request → wait → complete response | Open connection → stream both ways |
| **Latency** | 200–500 ms | Sub-100 ms |
| **Best for** | Batch jobs, file conversion | Voice agents, live transcription |
| **Complexity** | One `curl` call | Connection lifecycle to manage |

## How Each Protocol Works

**HTTP**

You send a request and get back the full result once processing finishes.

```mermaid theme={null}
sequenceDiagram
    participant Client
    participant SLNG
    Client->>SLNG: POST /v1/tts/slng/deepgram/aura:2-en<br/>{ "text": "Hello" }
    Note over SLNG: Generates full audio
    SLNG-->>Client: 200 OK – complete WAV file
```

**Use HTTP when you:**

* Generate audio files for download or storage
* Transcribe pre-recorded audio files
* Want the simplest possible integration
* Don't need real-time streaming

```bash theme={null}
curl https://api.slng.ai/v1/tts/slng/deepgram/aura:2-en \
  -H "Authorization: Bearer SLNG_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "model": "aura-2-thalia-en", "text": "Hello from HTTP!" }' \
  --output hello.wav
```

See complete examples: [TTS over HTTP](https://slng.mintlify.site/examples/tts-http.md) · [STT over HTTP](https://slng.mintlify.site/examples/stt-http.md)

**WebSocket**

You open a persistent connection and stream data in both directions.

```mermaid theme={null}
sequenceDiagram
    participant Client
    participant SLNG
    Client->>SLNG: Connect wss://api.slng.ai/v1/tts/slng/deepgram/aura:2-en
    SLNG-->>Client: Connection open
    Client->>SLNG: { type: "text", text: "Hello" }
    SLNG-->>Client: audio chunk 1
    SLNG-->>Client: audio chunk 2
    SLNG-->>Client: audio chunk 3
    Client->>SLNG: { type: "text", text: "More text" }
    SLNG-->>Client: audio chunk 4
    Note over Client,SLNG: Connection stays open – send more text anytime
```

**Use WebSocket when you:**

* Build voice agents (STT + LLM + TTS in a loop)
* Need real-time transcription from a microphone
* Want the lowest possible latency
* Stream audio continuously

```javascript theme={null}
const ws = new WebSocket("wss://api.slng.ai/v1/tts/slng/deepgram/aura:2-en");

ws.onopen = () => {
  ws.send(JSON.stringify({ type: "text", text: "Hello from WebSocket!" }));
};

ws.onmessage = (event) => {
  if (event.data instanceof ArrayBuffer) {
    playAudio(event.data); // Stream audio as it arrives
  }
};
```

See complete examples: [TTS over WebSocket](https://slng.mintlify.site/examples/tts-websocket.md) · [STT over WebSocket](https://slng.mintlify.site/examples/stt-websocket.md)

## WebSocket Best Practices

WebSocket connections are stateful. You need to handle the lifecycle:

* **Reconnect with backoff.** If the connection drops, retry with exponential delay (1s, 2s, 4s... up to 30s).
* **Handle binary and text frames.** Audio arrives as binary `ArrayBuffer`; control messages come as JSON text.
* **Send a close frame** when you're done so the server releases resources.

## Which Protocol for Which Use Case?

| Use case | Protocol | Why |
| - | - | - |
| Convert a script to an audio file | HTTP | One request, one file. Simple |
| Transcribe a batch of recordings | HTTP | Upload each file, get results |
| Voice agent (phone or web) | WebSocket | Real-time STT→LLM→TTS loop |
| Live captioning / transcription | WebSocket | Stream mic audio, get text back |
| Generate a voiceover for a video | HTTP | No real-time requirement |

## Next Steps

> **[TTS over HTTP](https://slng.mintlify.site/examples/tts-http.md)**
>
> Generate audio files from text with simple HTTP requests.

> **[TTS over WebSocket](https://slng.mintlify.site/examples/tts-websocket.md)**
>
> Stream audio in real-time with interruption support.

> **[STT over HTTP](https://slng.mintlify.site/examples/stt-http.md)**
>
> Transcribe pre-recorded audio files.

> **[STT over WebSocket](https://slng.mintlify.site/examples/stt-websocket.md)**
>
> Transcribe live audio from a microphone.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.