Skip to content

Commit 9ae6630

Browse files
committed
gpt-4.5'ed the docs
1 parent f868181 commit 9ae6630

File tree

1 file changed

+73
-94
lines changed

1 file changed

+73
-94
lines changed

fern/calls/websocket-transport.mdx

Lines changed: 73 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,43 @@
11
---
22
title: WebSocket Transport
3-
description: Stream audio directly via WebSockets for bidirectional real-time communication
3+
description: Stream audio directly via WebSockets for real-time, bidirectional communication
44
slug: calls/websocket-transport
55
---
66

77
# WebSocket Transport
88

9-
Vapi's WebSocket transport provides a powerful way to establish direct, bidirectional audio communication between your application and Vapi's AI assistants. Unlike traditional phone or web calls, this transport mechanism allows you to send and receive raw audio data in real time with minimal latency.
9+
Vapi's WebSocket transport enables real-time, bidirectional audio communication directly between your application and Vapi's AI assistants. Unlike traditional phone or web calls, this transport method lets you stream raw audio data instantly with minimal latency.
1010

11-
## Overview
11+
## Key Benefits
1212

13-
The WebSocket transport offers several advantages:
14-
15-
- **Low Latency**: Direct streaming reduces processing delays
16-
- **Bidirectional Communication**: Simultaneous audio streaming in both directions
17-
- **Flexible Integration**: Implement in any environment that supports WebSockets
18-
- **Customizable Audio Format**: Configure sample rate and format to match your needs
19-
- **Sample Rate Conversion**: Automatic handling of different audio sample rates
13+
- **Low Latency**: Direct streaming ensures minimal delays.
14+
- **Bidirectional Streaming**: Real-time audio flow in both directions.
15+
- **Easy Integration**: Compatible with any environment supporting WebSockets.
16+
- **Flexible Audio Formats**: Customize audio parameters such as sample rate.
17+
- **Automatic Sample Rate Conversion**: Seamlessly handles various audio rates.
2018

2119
## Creating a WebSocket Call
2220

23-
To create a call using the WebSocket transport:
21+
To initiate a call using WebSocket transport:
2422

2523
```bash
26-
curl 'https://api.vapi.ai/call'
27-
-H 'authorization: Bearer YOUR_API_KEY'
28-
-H 'content-type: application/json'
29-
--data-raw '{
30-
"assistant": {
31-
"assistantId": "YOUR_ASSISTANT_ID"
32-
},
33-
"transport": {
34-
"provider": "vapi.websocket",
35-
"audioFormat": {
36-
"format": "pcm_s16le",
37-
"container": "raw",
38-
"sampleRate": 16000
24+
curl 'https://api.vapi.ai/call' \
25+
-H 'authorization: Bearer YOUR_API_KEY' \
26+
-H 'content-type: application/json' \
27+
--data-raw '{
28+
"assistant": { "assistantId": "YOUR_ASSISTANT_ID" },
29+
"transport": {
30+
"provider": "vapi.websocket",
31+
"audioFormat": {
32+
"format": "pcm_s16le",
33+
"container": "raw",
34+
"sampleRate": 16000
35+
}
3936
}
40-
}
41-
}'
37+
}'
4238
```
4339

44-
### Sample Response
40+
### Sample API Response
4541

4642
```json
4743
{
@@ -62,101 +58,86 @@ curl 'https://api.vapi.ai/call'
6258

6359
## Audio Format Configuration
6460

65-
When creating a WebSocket call, you can configure the audio format:
61+
When creating a WebSocket call, the audio format can be customized:
6662

67-
| Parameter | Description | Default |
68-
|-----------|-------------|---------|
69-
| `format` | Audio encoding format | `pcm_s16le` (16-bit PCM) |
70-
| `container` | Audio container format | `raw` (Raw PCM) |
71-
| `sampleRate` | Sample rate in Hz | `16000` (16kHz) |
63+
| Parameter | Description | Default |
64+
|-------------|-------------------------|---------------------|
65+
| `format` | Audio encoding format | `pcm_s16le` (16-bit PCM) |
66+
| `container` | Audio container format | `raw` (Raw PCM) |
67+
| `sampleRate`| Sample rate in Hz | `16000` (16kHz) |
7268

7369
<Important>
74-
Currently, only raw PCM audio data (`pcm_s16le` format with `raw` container) is supported. Additional audio formats and container types may be supported in future releases.
70+
Currently, Vapi supports only raw PCM (`pcm_s16le` with `raw` container). Additional formats may be supported in future updates.
7571
</Important>
7672

7773
<Note>
78-
Vapi automatically handles sample rate conversion between your specified rate and the model's required format. This means you can send audio at 8kHz, 44.1kHz, or other rates, and Vapi will convert it appropriately.
74+
Vapi automatically converts sample rates as needed. You can stream audio at 8kHz, 44.1kHz, etc., and Vapi will handle conversions seamlessly.
7975
</Note>
8076

8177
## Connecting to the WebSocket
8278

83-
After creating a call, connect to the WebSocket URL returned in the response:
79+
Use the WebSocket URL from the response to establish a connection:
8480

8581
```javascript
86-
// Using the websocketCallUrl from the response
8782
const socket = new WebSocket("wss://api.vapi.ai/7420f27a-30fd-4f49-a995-5549ae7cc00d/transport");
8883

89-
socket.onopen = () => {
90-
console.log("WebSocket connection established");
91-
};
92-
93-
socket.onclose = () => {
94-
console.log("WebSocket connection closed");
95-
};
96-
97-
socket.onerror = (error) => {
98-
console.error("WebSocket error:", error);
99-
};
84+
socket.onopen = () => console.log("WebSocket connection opened.");
85+
socket.onclose = () => console.log("WebSocket connection closed.");
86+
socket.onerror = (error) => console.error("WebSocket error:", error);
10087
```
10188

10289
## Sending and Receiving Data
10390

104-
The WebSocket connection supports two types of messages:
91+
The WebSocket supports two types of messages:
10592

106-
1. **Binary audio data**: Raw PCM audio frames (16-bit signed little-endian format)
107-
2. **Text messages**: JSON-formatted control messages
93+
- **Binary audio data** (PCM, 16-bit signed little-endian)
94+
- **Text-based JSON control messages**
10895

10996
### Sending Audio Data
11097

11198
```javascript
112-
// Send binary audio data
11399
function sendAudioChunk(audioBuffer) {
114100
if (socket.readyState === WebSocket.OPEN) {
115101
socket.send(audioBuffer);
116102
}
117103
}
118104

119-
// Example: Send audio from a microphone stream
120-
navigator.mediaDevices.getUserMedia({ audio: true })
121-
.then(stream => {
122-
const audioContext = new AudioContext();
123-
const source = audioContext.createMediaStreamSource(stream);
124-
const processor = audioContext.createScriptProcessor(1024, 1, 1);
125-
126-
processor.onaudioprocess = (e) => {
127-
const pcmData = e.inputBuffer.getChannelData(0);
128-
// Convert Float32Array to Int16Array (for pcm_s16le format)
129-
const int16Data = new Int16Array(pcmData.length);
130-
for (let i = 0; i < pcmData.length; i++) {
131-
int16Data[i] = Math.max(-32768, Math.min(32767, pcmData[i] * 32768));
132-
}
133-
sendAudioChunk(int16Data.buffer);
134-
};
135-
136-
source.connect(processor);
137-
processor.connect(audioContext.destination);
138-
});
105+
navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
106+
const audioContext = new AudioContext();
107+
const source = audioContext.createMediaStreamSource(stream);
108+
const processor = audioContext.createScriptProcessor(1024, 1, 1);
109+
110+
processor.onaudioprocess = (event) => {
111+
const pcmData = event.inputBuffer.getChannelData(0);
112+
const int16Data = new Int16Array(pcmData.length);
113+
114+
for (let i = 0; i < pcmData.length; i++) {
115+
int16Data[i] = Math.max(-32768, Math.min(32767, pcmData[i] * 32768));
116+
}
117+
118+
sendAudioChunk(int16Data.buffer);
119+
};
120+
121+
source.connect(processor);
122+
processor.connect(audioContext.destination);
123+
});
139124
```
140125

141-
### Receiving Messages
126+
### Receiving Data
142127

143128
```javascript
144129
socket.onmessage = (event) => {
145130
if (event.data instanceof Blob) {
146-
// Handle binary audio data
147131
event.data.arrayBuffer().then(buffer => {
148132
const audioData = new Int16Array(buffer);
149-
// Process audio data (e.g., play it back or analyze it)
150133
playAudio(audioData);
151134
});
152135
} else {
153-
// Handle JSON control messages
154136
try {
155137
const message = JSON.parse(event.data);
156-
console.log("Received control message:", message);
157138
handleControlMessage(message);
158139
} catch (error) {
159-
console.error("Error parsing message:", error);
140+
console.error("Failed to parse message:", error);
160141
}
161142
}
162143
};
@@ -171,37 +152,35 @@ function sendControlMessage(messageObj) {
171152
}
172153
}
173154

174-
// Example: Send a hangup message
155+
// Example: hangup call
175156
function hangupCall() {
176157
sendControlMessage({ type: "hangup" });
177158
}
178159
```
179160

180161
## Ending the Call
181162

182-
To properly end a WebSocket call:
163+
To gracefully end the WebSocket call:
183164

184165
```javascript
185-
// Send hangup message
186166
sendControlMessage({ type: "hangup" });
187-
188-
// Close the WebSocket connection
189167
socket.close();
190168
```
191169

192-
## Comparison with Call Listen Feature
170+
## Comparison: WebSocket Transport vs. Call Listen Feature
193171

194-
Vapi offers two WebSocket-related features that serve different purposes:
172+
Vapi provides two WebSocket options:
195173

196-
| WebSocket Transport | Call Listen Feature |
197-
|---------------------|---------------------|
198-
| Primary communication channel for the call | Secondary monitoring channel for an existing call |
199-
| Bidirectional audio streaming | One-way audio streaming (receive only) |
200-
| Replaces phone/web as the transport method | Supplements an existing phone/web call |
201-
| Created with `transport.provider: "vapi.websocket"` | Accessed via the `monitor.listenUrl` in a standard call |
174+
| WebSocket Transport | Call Listen Feature |
175+
|-------------------------------------|------------------------------------|
176+
| Primary communication method | Secondary, monitoring-only channel |
177+
| Bidirectional audio streaming | Unidirectional (listen-only) |
178+
| Replaces phone/web as transport | Supplements existing calls |
179+
| Uses `provider: "vapi.websocket"` | Accessed via `monitor.listenUrl` |
202180

203-
See [Live Call Control](/calls/call-features) for more information about the Call Listen feature.
181+
Refer to [Live Call Control](/calls/call-features) for more on the Call Listen feature.
204182

205183
<Warning>
206-
When using the WebSocket transport, you cannot simultaneously use phone number parameters (`phoneNumber` or `phoneNumberId`). The transport method is mutually exclusive with phone-based calling.
207-
</Warning>
184+
When using WebSocket transport, phone-based parameters (`phoneNumber` or `phoneNumberId`) are not permitted. These methods are mutually exclusive.
185+
</Warning>
186+

0 commit comments

Comments
 (0)