> This is a page from the ElevenLabs documentation. For a complete page index, fetch https://elevenlabs.io/docs/llms.txt. For the full documentation in a single file, fetch https://elevenlabs.io/docs/llms-full.txt.

# रियल-टाइम में डायलॉग स्ट्रीम करें

टेक्स्ट टू डायलॉग WebSocket (`/v1/text-to-dialogue/stream-input`) एक ही कनेक्शन खुला रखता है, जबकि आप डायलॉग लाइनें भेजते हैं और base64-encoded ऑडियो chunks पाते हैं। यह सिर्फ़ **Eleven v3** और **Eleven v4** डायलॉग मॉडल के लिए है (`model_id` की शुरुआत `eleven_v3` या `eleven_v4` से होनी चाहिए)।

> **Note**
>
> इस गाइड में **टेक्स्ट टू डायलॉग** WebSocket बताया गया है। **Flash**, **Multilingual v2**, या दूसरे
> non-v3 TTS मॉडल के लिए [रीयलटाइम TTS WebSocket](/docs/hi/eleven-api/guides/how-to/websockets/realtime-tts) इस्तेमाल करें। दोनों प्रोटोकॉल का साथ-साथ सारांश देखने के लिए
> [टेक्स्ट टू स्पीच और टेक्स्ट टू डायलॉग WebSockets](/docs/hi/eleven-api/guides/how-to/websockets/tts-vs-ttd-websockets) देखें।

## ज़रूरी चीज़ें

* API key वाला ElevenLabs अकाउंट ([authentication](/docs/hi/api-reference/authentication))।
* API key में `Text to Speech` permissions होनी चाहिए।
* आपकी मशीन पर Python या Node.js इंस्टॉल होना चाहिए।

## सेटअप

**`पायथन`**

```python पायथन
pip install python-dotenv websockets
```

**`टाइपस्क्रिप्ट`**

```typescript टाइपस्क्रिप्ट
npm install dotenv ws
npm install @types/dotenv @types/ws --save-dev
```

एक `.env` फ़ाइल बनाएं:

**`.env`**

```bash .env
ELEVENLABS_API_KEY=your_elevenlabs_api_key_here
```

[वॉइस लाइब्रेरी](https://elevenlabs.io/voice-library) से एक **voice ID** चुनें। नीचे दिए उदाहरणों में `eleven_v4_turbo` इस्तेमाल किया गया है, जो हर कनेक्शन के लिए **एक** registered voice की अनुमति देता है।

## WebSocket खोलें

`model_id` और `output_format` जैसे query parameters के साथ `wss://api.elevenlabs.io/v1/text-to-dialogue/stream-input` से कनेक्ट करें। आप API key को `xi-api-key` header में या पहले JSON message में भेज सकते हैं (भाषाओं में एक समान पैटर्न के लिए यहाँ इसे body में दिखाया गया है)।

**`text-to-dialogue-websocket.py`**

```python text-to-dialogue-websocket.py
import asyncio
import base64
import json
import os

from dotenv import load_dotenv
import websockets

load_dotenv()
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
VOICE_ID = "21m00Tcm4TlvDq8ikWAM"
MODEL_ID = "eleven_v4_turbo"

URI = (
    "wss://api.elevenlabs.io/v1/text-to-dialogue/stream-input"
    f"?model_id={MODEL_ID}&output_format=mp3_44100_128"
)
```

**`text-to-dialogue-websocket.ts`**

```typescript text-to-dialogue-websocket.ts
import * as dotenv from "dotenv";
import * as fs from "node:fs";
import WebSocket from "ws";

dotenv.config();
const ELEVENLABS_API_KEY = process.env.ELEVENLABS_API_KEY;
const voiceId = "21m00Tcm4TlvDq8ikWAM";
const modelId = "eleven_v4_turbo";

const uri = `wss://api.elevenlabs.io/v1/text-to-dialogue/stream-input?model_id=${modelId}&output_format=mp3_44100_128`;
const websocket = new WebSocket(uri);

const outputDir = "./output";
try {
  fs.accessSync(outputDir, fs.constants.R_OK | fs.constants.W_OK);
} catch {
  fs.mkdirSync(outputDir);
}
const writeStream = fs.createWriteStream(`${outputDir}/dialogue-ws.mp3`, { flags: "a" });
```

## वॉइस रजिस्टर करें और टेक्स्ट स्ट्रीम करें

एक **पहला message** भेजें, जिसमें `voices` (ज़रूरी) और, अगर आपने `xi-api-key` header सेट नहीं किया है, तो `xi_api_key` शामिल हो। फिर `inputs` के साथ एक या अधिक frames भेजें: हर item में `text`, `voice_id`, और वैकल्पिक `new_turn` होता है।

सर्वर तब तक टेक्स्ट buffer करता है, जब तक उसके पास पर्याप्त context (लगभग **40 characters** और **8 words**) न हो जाए। फिर वह `audio` chunks भेजता है। Response fields में **snake_case** इस्तेमाल होता है (उदाहरण के लिए `is_final`)।

**`text-to-dialogue-websocket.py`**

```python text-to-dialogue-websocket.py
async def stream_dialogue():
    async with websockets.connect(URI) as websocket:
        await websocket.send(
            json.dumps(
                {
                    "voices": [VOICE_ID],
                    "xi_api_key": ELEVENLABS_API_KEY,
                }
            )
        )

        line = (
            "This is a longer line of dialogue used to exceed the minimum buffer so the model "
            "starts generating streamed audio for the registered voice. "
        )
        await websocket.send(
            json.dumps(
                {
                    "inputs": [
                        {"text": line, "voice_id": VOICE_ID, "new_turn": False},
                    ],
                }
            )
        )

        await websocket.send(json.dumps({"close_socket": True}))

        os.makedirs("output", exist_ok=True)
        out_path = "output/dialogue-ws.mp3"
        with open(out_path, "wb") as audio_file:
            while True:
                raw = await websocket.recv()
                msg = json.loads(raw)
                if msg.get("error"):
                    raise RuntimeError(msg)
                if msg.get("audio"):
                    audio_file.write(base64.b64decode(msg["audio"]))
                if msg.get("is_final"):
                    break
        print(f"Wrote {out_path}")


asyncio.run(stream_dialogue())
```

**`text-to-dialogue-websocket.ts`**

```typescript text-to-dialogue-websocket.ts
websocket.on("open", () => {
  websocket.send(
    JSON.stringify({
      voices: [voiceId],
      xi_api_key: ELEVENLABS_API_KEY,
    })
  );

  const line =
    "This is a longer line of dialogue used to exceed the minimum buffer so the model starts generating streamed audio for the registered voice. ";

  websocket.send(
    JSON.stringify({
      inputs: [{ text: line, voice_id: voiceId, new_turn: false }],
    })
  );

  websocket.send(JSON.stringify({ close_socket: true }));
});

websocket.on("message", (data) => {
  const msg = JSON.parse(data.toString());
  if (msg.error) {
    console.error(msg);
    return;
  }
  if (msg.audio) {
    writeToLocal(msg.audio, writeStream);
  }
});

function writeToLocal(base64str: string, stream: fs.WriteStream) {
  stream.write(Buffer.from(base64str, "base64"));
}

websocket.on("close", () => {
  writeStream.end();
});
```

`close_socket` किसी भी buffered text को flush करता है, बचा हुआ ऑडियो भेजता है, और कनेक्शन बंद होने से पहले `is_final: true` के साथ एक अंतिम frame भेजता है। लाइनों के बीच **कनेक्शन खुला रखने** के लिए, session खत्म होने तक `close_socket` न भेजें; socket बंद किए बिना छोटे buffers के लिए ऑडियो ज़बरदस्ती जनरेट करने हेतु `flush` इस्तेमाल करें।

## स्क्रिप्ट चलाएं

**`पायथन`**

```python पायथन
python text-to-dialogue-websocket.py
```

**`टाइपस्क्रिप्ट`**

```typescript टाइपस्क्रिप्ट
npx tsx text-to-dialogue-websocket.ts
```

आपको `output/` के अंदर एक MP3 फ़ाइल मिलनी चाहिए (फ़ाइलनाम ऊपर दिए उदाहरण जैसा होगा)।

## व्यवहार संबंधी नोट्स

### बफ़रिंग

TTS WebSocket के `chunk_length_schedule` के उलट, डायलॉग स्ट्रीमिंग पहले partial audio से पहले **निश्चित server threshold** (character और word count) इस्तेमाल करती है। अगर आप छोटी लाइनें भेजते हैं और देरी सुनाई देती है, तो हर `inputs` frame में थोड़ा ज़्यादा टेक्स्ट batch करें या socket बंद किए बिना जनरेशन के लिए `flush: true` भेजें।

### टर्न और वॉइस

जब कोई speaker अपना turn पूरा करे, तो `new_turn: true` सेट करें, ताकि prosody साफ़ तरीके से reset हो सके। `inputs` entries के बीच `voice_id` बदलने पर भी नया turn शुरू होता है। `eleven_v4_turbo` के साथ `voices` में **ठीक एक** voice रजिस्टर करें; `eleven_v4` अधिकतम **10** registered voices को सपोर्ट करता है।

### निष्क्रियता

अगर सर्वर को **20 seconds तक कोई client message नहीं मिलता**, तो कनेक्शन खत्म हो जाता है। ऑडियो synthesize किए बिना timer reset करने के लिए `{"keep_alive": true}` भेजें।

### समवर्ती कनेक्शन

हर खुला कनेक्शन, जब तक खुला रहता है, एक डायलॉग session रखता है। यह आपके प्लान की standard concurrency limit से अलग dedicated pool से लिया जाता है। कनेक्शन पर जनरेट किया गया ऑडियो standard concurrency में नहीं गिना जाता। [टेक्स्ट टू डायलॉग concurrency](/docs/hi/overview/models#text-to-dialogue-concurrency) देखें।

### अलाइनमेंट

जहाँ उपलब्ध हो, chunks पर `alignment` objects (snake_case timing arrays) पाने के लिए query string में `sync_alignment=true` जोड़ें। [API रेफरेंस](/docs/hi/api-reference/text-to-dialogue/ttd-websocket) देखें।

## अगले चरण

#### [TTS और TTD WebSockets](/docs/hi/eleven-api/guides/how-to/websockets/tts-vs-ttd-websockets)

सही WebSocket चुनें और message shapes की तुलना करें।

#### [टेक्स्ट टू डायलॉग WebSocket (API)](/docs/hi/api-reference/text-to-dialogue/ttd-websocket)

Query parameters, message schemas और उदाहरण।

#### [डायलॉग स्ट्रीम करें (HTTP)](/docs/hi/api-reference/text-to-dialogue/stream)

जब WebSocket के बिना पूरा request text उपलब्ध हो।

#### [रीयलटाइम TTS WebSocket](/docs/hi/eleven-api/guides/how-to/websockets/realtime-tts)

WebSockets पर single-voice non-v3 streaming।