Web Responder Media Streaming
Stream live call audio to your WebSocket server for transcription, analysis, or voice agents.
The <Stream> verb sends a call's audio to your WebSocket server in near real time. Your application can listen to the call, and with a two-way stream, send audio back. This makes it possible to build real-time transcription, call analysis, or AI voice agents.
Choosing a stream type
| Background stream | Two-way stream | |
|---|---|---|
| Verb | <Stream> inside <Start> | <Stream> inside <Connect> |
| Direction | Call audio to your server only | Both directions |
| Call flow | The call continues with the next verb while audio streams | The Web Responder waits on the stream until it stops |
| Typical use | Live transcription, keyword detection, analytics | Voice agents and other interactive applications |
Background stream
<Start> begins streaming and moves straight on to the next verb, so the call isn't interrupted. Always follow it with more verbs; if nothing else is left to run, the call may end.
<Response>
<Start>
<Stream name="my_stream" url="wss://example.com/audio"/>
</Start>
<Say>This plays while the audio streams.</Say>
</Response>To stop a background stream before the call ends, give it a name when you start it, then return a <Stop> with the same name:
<Response>
<Stop>
<Stream name="my_stream"/>
</Stop>
</Response>Two-way stream
<Connect> opens a stream your application can both listen to and respond on:
<Connect>
<Stream url="wss://example.com/audio"/>
</Connect>To end it, send a stop event from your server over the WebSocket. The stream closes, and the Web Responder moves on to its next action:
{
"event": "stop"
}Stream attributes
| Attribute | Description |
|---|---|
url | Your WebSocket URL. Use a secure wss:// URL. |
track | Which audio to stream: inbound (audio from the caller), outbound (audio sent to the caller, such as an agent or prompts), or both (the whole conversation). |
name | An identifier for the stream. Required if you want to stop a background stream with <Stop>, and useful for telling multiple streams apart. |
Custom parameters
Add <Parameter> elements to pass your own data to the WebSocket server. They arrive in the customParameters object of the start message (see WebSocket messages).
<Stream name="word_detection" url="wss://example.com/audio" track="both">
<Parameter name="customerID" value="12345"/>
<Parameter name="callType" value="support"/>
</Stream>| Attribute | Description |
|---|---|
name | The parameter's key |
value | The parameter's value |
WebSocket messages
Every message on the WebSocket is a JSON string. The event field tells you which kind it is. A stream sends messages in this order: connected, then start, then a series of media messages, then stop.
connected
Sent first, as soon as the WebSocket connection opens.
| Field | Description |
|---|---|
event | connected |
version | The protocol version |
{
"event": "connected",
"version": "1.0.0"
}start
Sent once, right after connected, with details about the stream.
| Field | Description |
|---|---|
event | start |
sequenceNumber | Message order, starting at 1 |
start.stream_id | Unique ID of this stream |
start.call_id | ID of the call being streamed |
start.tracks | The tracks being streamed |
start.customParameters | Any custom parameters from the <Stream> verb |
start.mediaFormat | The audio format: encoding, sampleRate, and channels |
{
"event": "start",
"sequenceNumber": "1",
"start": {
"stream_id": "2d9be7d7-4a66-4667-b2e1-cf3fa4e66306",
"call_id": "vmj74pd5nmm9jlukvbfg",
"tracks": ["inbound", "outbound"],
"customParameters": {
"customerID": "12345",
"callType": "support"
},
"mediaFormat": {
"encoding": "audio/x-mulaw",
"sampleRate": 8000,
"channels": 1
}
}
}Use mediaFormat to decode the audio in the media messages. In this example, it's 8 kHz, mono, µ-law.
media
Carries a chunk of audio.
| Field | Description |
|---|---|
event | media |
sequenceNumber | Increases with every message |
media.track | Which track the audio belongs to: inbound or outbound |
media.chunk | The chunk's position on its track, starting at 1. Use it to keep audio in order. |
media.payload | The audio, base64-encoded |
media.timestamp | The chunk's timestamp |
{
"event": "media",
"sequenceNumber": "3",
"media": {
"track": "outbound",
"chunk": "1",
"payload": "base64AudioData",
"timestamp": "1708388604"
}
}stop
Sent when the stream ends or the call hangs up.
| Field | Description |
|---|---|
event | stop |
sequenceNumber | Increases with every message |
stop.call_id | ID of the call |
stop.stream_id | ID of the stream |
stop.tracks[].type | The track: inbound or outbound |
stop.tracks[].bytesSent | Total audio data sent on that track |
stop.tracks[].duration | How long that track streamed, in seconds |
{
"event": "stop",
"sequenceNumber": "416",
"stop": {
"tracks": [
{ "type": "inbound", "bytesSent": 66240, "duration": 8 },
{ "type": "outbound", "bytesSent": 99114, "duration": 10 }
],
"call_id": "24j83iflgottdokd39vt",
"stream_id": "6ccf2088-6755-4861-a162-3c6c30d6ad07"
}
}Limitations
Recording while streamingCall recording doesn't capture the Web Responder's own leg while
<Stream>is in use, because streamed audio doesn't pass through the usual media path. Record the caller's leg or the connection instead.
For a working WebSocket server, see the streaming example.
Updated 11 days ago
