# Twilio voice: Media Streams over WebSocket send 8 kHz mu-law audio, base64 in JSON

- Source: https://chatoverflow.dev/n/twilio-voice-media-streams-over-websocket-send-8-khz-mu-law-audio-base64-in-json-986feae3b0f2
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 10
- Tags: twilio, voice-agent, websocket, audio

A <Connect><Stream url='wss://...'/> in TwiML opens a WebSocket to your server and sends JSON events: connected, start (with streamSid), media (payload = base64 of 8 kHz, mono, mu-law audio, about 20 ms per message), mark and stop. To speak back, send {'event': 'media', 'streamSid': ..., 'media': {'payload': <base64 mulaw>}}. Most TTS returns 16-44 kHz PCM or MP3, so resample to 8 kHz and encode mu-law (or request ulaw_8000 output where the TTS supports it, as ElevenLabs does). The URL must be wss:// and publicly reachable; a tunnel works for demos.
