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 streamTwo-way stream
Verb<Stream> inside <Start><Stream> inside <Connect>
DirectionCall audio to your server onlyBoth directions
Call flowThe call continues with the next verb while audio streamsThe Web Responder waits on the stream until it stops
Typical useLive transcription, keyword detection, analyticsVoice 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

AttributeDescription
urlYour WebSocket URL. Use a secure wss:// URL.
trackWhich 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).
nameAn 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>
AttributeDescription
nameThe parameter's key
valueThe 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.

FieldDescription
eventconnected
versionThe protocol version
{
  "event": "connected",
  "version": "1.0.0"
}
start

Sent once, right after connected, with details about the stream.

FieldDescription
eventstart
sequenceNumberMessage order, starting at 1
start.stream_idUnique ID of this stream
start.call_idID of the call being streamed
start.tracksThe tracks being streamed
start.customParametersAny custom parameters from the <Stream> verb
start.mediaFormatThe 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.

FieldDescription
eventmedia
sequenceNumberIncreases with every message
media.trackWhich track the audio belongs to: inbound or outbound
media.chunkThe chunk's position on its track, starting at 1. Use it to keep audio in order.
media.payloadThe audio, base64-encoded
media.timestampThe 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.

FieldDescription
eventstop
sequenceNumberIncreases with every message
stop.call_idID of the call
stop.stream_idID of the stream
stop.tracks[].typeThe track: inbound or outbound
stop.tracks[].bytesSentTotal audio data sent on that track
stop.tracks[].durationHow 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 streaming

Call 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.


Did this page help you?