Skip to content

WebSocket protocol

This page describes the messages exchanged over a stream’s WebSocket connection. The same protocol is published as a machine-readable AsyncAPI 3.0 document.

SIPRTC is the WebSocket client and your server is the WebSocket server.

  • SIPRTC sends an HTTP GET upgrade request to the path and query string of your url, with your host name in the Host header. Don’t rely on the Origin header.
  • The request offers the subprotocol ws-protocol in Sec-WebSocket-Protocol. Accept it: answer with Sec-WebSocket-Protocol: ws-protocol. The examples in Audio formats and examples show how.
  • SIPRTC sends no Authorization header and no custom headers. See Security.
  • Compression extensions such as permessage-deflate are refused, to keep latency low.
  • Your server must complete the handshake (HTTP 101 Switching Protocols) within about 10 seconds.

All messages in both directions are JSON objects in text frames, encoded as UTF-8. Audio is carried inside the JSON as base64. SIPRTC ignores binary frames. Messages longer than 4 KB may arrive as a fragmented WebSocket message; standard WebSocket libraries reassemble these for you.

SIPRTC doesn’t send WebSocket pings, but it answers yours. While a stream is running, media messages normally arrive every 20 ms, so the connection is rarely idle.

For each stream, SIPRTC sends:

  1. connected, once.
  2. start, once (and again if you ask for it with startreq).
  3. media for each packet of audio, and dtmf and mark as they happen.
  4. stop, once, if SIPRTC ends the stream. You don’t get stop if your server closed the connection.

Ignore any media that arrives before start.

SIPRTCYour serverconnectedstart · call, tracks, media_formatmedia · media · media… (every 20 ms)dtmf · when the caller presses a keymedia · your audiosent as fast as you have itmark "greeting-done"mark "greeting-done" · receivedmark "greeting-done" · played (or cleared)clear · barge-incall ends or <Stop><Stream/>stop, then close 1000
Messages on one two-way stream, in order
Field Type Description
event string The message type.
sequence_number string A decimal counter, "1" for the first start message, increased by one for every message SIPRTC sends on this stream. The counter is shared by all message types, so media sequence numbers have gaps where other messages were sent. connected has no sequence number.
stream_sid string Identifies the stream. A UUID that is also sent as StreamSid in status callbacks.

Note that sequence_number and media.timestamp are strings containing numbers.

The first message on every connection.

{
"event": "connected",
"protocol": "Call",
"version": "1.0.0"
}
Field Type Description
protocol string Always Call.
version string Protocol version, currently 1.0.0.

Describes the call and the audio format. It follows connected. Read the audio format from this message before you decode any audio.

{
"event": "start",
"sequence_number": "1",
"start": {
"account_sid": "AC0000000000000000000",
"call_sid": "4f9a2c3e-1b7d-4e8a-9c6f-0d2e5b7a1c94",
"to": "+15550100123",
"from": "+15550100987",
"stream_sid": "8b6f0d2a-3c4e-4f1a-9b7d-2e5c6a1f0b3d",
"tracks": ["inbound"],
"media_format": {
"encoding": "audio/x-mulaw",
"sample_rate": 8000,
"channels": 1
},
"audioType": "text"
},
"custom_parameters": {
"customer_id": "C-1042",
"language": "en-US"
},
"stream_sid": "8b6f0d2a-3c4e-4f1a-9b7d-2e5c6a1f0b3d"
}
Field Type Description
start.account_sid string Your account.
start.call_sid string The call being streamed. Use it with the Calls API, for example to redirect the call when your bot hands over.
start.to string The number or address that was called.
start.from string The caller ID of the call.
start.stream_sid string Same as the top-level stream_sid.
start.tracks array of strings The tracks you will receive: ["inbound"], ["outbound"] or ["inbound", "outbound"].
start.media_format.encoding string audio/x-mulaw or audio/L16. Used in both directions.
start.media_format.sample_rate number 8000 or 16000. Used in both directions.
start.media_format.channels number Always 1.
start.audioType string Always text: audio is base64 inside JSON text messages.
custom_parameters object or null The JSON object from the content of the <Stream> element, unchanged. null if there was none. Note that it is a top-level field, not inside start.

One packet of call audio. The example is 20 ms of μ-law silence. Packets are normally 20 ms long, but follow the call’s packet size, so don’t depend on an exact length.

{
"event": "media",
"sequence_number": "2",
"media": {
"timestamp": "20",
"track": "inbound",
"payload": "/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////w=="
},
"stream_sid": "8b6f0d2a-3c4e-4f1a-9b7d-2e5c6a1f0b3d"
}
Field Type Description
media.timestamp string Milliseconds since the start message was sent, measured when this packet was sent. Use it to line up the two tracks.
media.track string inbound (audio from the other party on this call leg) or outbound (audio sent to them).
media.payload string Base64 audio in the encoding and sample rate from start.media_format. At 20 ms: 160 bytes of μ-law, 320 bytes of L16 at 8000 Hz, or 640 bytes of L16 at 16000 Hz. See Audio formats.

With both_tracks, inbound and outbound packets are separate messages. The outbound track carries audio only while SIPRTC is sending audio to the party, so it can have gaps.

A key the caller pressed. One message per key. Sent on one-way and two-way streams.

{
"event": "dtmf",
"stream_sid": "8b6f0d2a-3c4e-4f1a-9b7d-2e5c6a1f0b3d",
"sequence_number": "57",
"dtmf": {
"track": "inbound_track",
"digit": "5"
}
}
Field Type Description
dtmf.track string Always inbound_track.
dtmf.digit string One of 0–9, *, #, A–D.

While a stream is running, key presses are sent to your server. Don’t rely on <Gather> collecting digits on a call leg that is streaming.

Two-way streams only. The echo of a mark you sent; see Marks.

{
"event": "mark",
"sequence_number": "88",
"stream_sid": "8b6f0d2a-3c4e-4f1a-9b7d-2e5c6a1f0b3d",
"mark": {
"name": "greeting-end"
}
}
Field Type Description
mark.name string The name you gave the mark.

SIPRTC is ending the stream: the call hung up, or TinyML ran <Stop><Stream/></Stop>. The connection is closed with code 1000 right after it.

{
"event": "stop",
"sequence_number": "412",
"stop": {
"account_sid": "AC0000000000000000000",
"call_sid": "4f9a2c3e-1b7d-4e8a-9c6f-0d2e5b7a1c94"
},
"stream_sid": "8b6f0d2a-3c4e-4f1a-9b7d-2e5c6a1f0b3d"
}
Field Type Description
stop.account_sid string Your account.
stop.call_sid string The call.

Messages you send are acted on only in two-way streams (<Connect><Stream>). In a one-way stream, SIPRTC ignores what you send, except that text which isn’t valid JSON is passed to your status callback as stream-json.

SIPRTC doesn’t check stream_sid, sequence_number or other fields that aren’t listed below, so you may include them or leave them out.

Audio to play to the caller. The example is 20 ms of silence in audio/x-mulaw.

{
"event": "media",
"media": {
"payload": "/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////w=="
}
}
Field Type Required Description
event string Yes media
media.payload string Yes Base64 audio in the stream’s encoding and sample rate (from start.media_format), mono. Any length; whole samples are best.
media.track string No Leave it out, or set it to outbound. Audio sent with "track": "inbound" is not played to the caller.

See Playback for how SIPRTC queues and plays your audio.

SIPRTC also accepts audio in this form. It is played exactly like a media message.

{
"type": "streamAudio",
"data": {
"audioData": "/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////w=="
}
}
Field Type Required Description
type string Yes streamAudio
data.audioData string Yes Base64 audio, as for media.payload. Other fields in data are ignored; the stream’s encoding and rate always apply.

Places a marker after the audio you’ve sent so far.

{
"event": "mark",
"mark": {
"name": "greeting-end"
}
}
Field Type Required Description
mark.name string Yes A name for the mark. Use letters, digits, - and _: SIPRTC copies the name into its echo without escaping it.
mark.track string No Leave it out, or set it to outbound.

Barge-in: drops all the audio you’ve queued that hasn’t been played yet. Send it when the caller starts talking over your bot.

{
"event": "clear"
}
Field Type Required Description
track string No Leave it out, or set it to outbound.

The caller hears silence until you send new audio. Every mark that was still waiting is echoed straight away.

Sends DTMF tones to the caller, for example to drive an automated system on the other end of the call.

{
"event": "dtmf",
"dtmf": {
"digit": "1234#"
}
}
Field Type Required Description
dtmf.digit string Yes One or more of 0–9, *, #, A–D, sent in order.

Asks SIPRTC to send the start message again, for example if your server lost its state for the connection. The new start has a new sequence number and the same content.

{
"event": "startreq"
}

In a two-way stream, any text message other than media, streamAudio, mark and clear (including dtmf, startreq, unknown events and text that isn’t JSON) is also passed, unchanged, to your status callback as a stream-json event, if the stream has a statusCallback.

In a two-way stream SIPRTC queues the audio you send and plays it to the caller at the call’s pace:

  • Send audio as soon as you have it. You don’t need to pace it in real time. Up to 30 seconds can be queued; audio beyond that is dropped.
  • Playback starts once about 100 ms is queued, and restarts the same way after the queue runs empty or is cleared. This absorbs network jitter. A sound shorter than that waits until more audio arrives, so follow short sounds with silence or more speech.
  • Audio plays in whole packets (normally 20 ms). A trailing part-packet waits for more audio.
  • When nothing is queued, the caller hears silence. SIPRTC keeps sending silent packets so the call stays up.
  • If the sample rate of the call differs from the stream’s rate, SIPRTC converts your audio. Always send audio at start.media_format.sample_rate.

A mark tells you when playback has reached a point in your audio, for example the end of a sentence:

  1. Send media with your audio, then mark with a name.
  2. SIPRTC echoes a mark message with that name as soon as it receives your mark.
  3. SIPRTC echoes the same mark again when playback reaches it, or when a clear drops the audio before it.

So expect two echoes per mark: the second one means the audio before the mark has been played (or cleared). If no audio is queued when the mark arrives, both echoes come almost at once.

  • SIPRTC closes the connection with code 1000 (normal closure) after sending stop, when the call hangs up or TinyML runs <Stop><Stream/></Stop>.
  • Your server can close the connection at any time with any code. The stream ends and isn’t reopened. In a two-way stream the call stays connected with no audio until you redirect it or hang it up through the Calls API.
  • No reconnection. If the connection fails or drops, the stream is over.
  • Invalid messages don’t close the connection. They are ignored, or passed to your status callback as described in Other messages.

To see why a connection failed, use the stream-error status callback.