Module 30: Building a Custom Streaming Client
Theoryβ
Figure: Building real-time bidirectional voice streaming clients communicating over WebSockets with the Gemini Live API.
Beyond the Dev UIβ
The ADK Developer UI (uv run adk web) has a microphone button built in, giving you bidirectional voice streaming with your agent for free, with no client code of your own. It's a great way to demo an agent, but the Dev UI is a tool for development, not a production-ready application.
To integrate ADK's voice streaming capabilities into your own website or application, you need to build a custom streaming client. This involves creating a front-end (for example, using HTML and JavaScript) that communicates directly with the ADK API server over WebSockets using a Gemini Live model (gemini-3.8-live).
The Architecture of a Streaming Applicationβ
A custom streaming application consists of two main components that communicate in real-time:
-
The ADK Server: Run headlessly from your parent
adk-trainingworkspace withuv run adk api_server. For real-time bidirectional voice and video, it exposes a WebSocket endpoint at/run_live(backed bygemini-3.8-live). -
The Custom Client: The user-facing web page (served from a separate static folder so backend
.envfiles stay private), responsible for:- Unlocking browser audio playback (
await playbackAudioContext.resume()) inside the user's click/press gesture so browser autoplay policies never mute the agent's voice. - Creating a session on the server (
POST /apps/{app_name}/users/{user_id}/sessions) before opening the WebSocket. - Capturing audio from the user's microphone as raw 16kHz PCM samples via an
AudioWorklet. - Streaming those PCM chunks over the
/run_liveWebSocket and playing the 24kHz PCM audio chunks returned by the server.
- Unlocking browser audio playback (
A note on browser security: Because the static client (
http://localhost:8081) and the ADK API server (http://localhost:8000) run on different ports, this is a cross-origin setup. Startadk api_serverwith--allow_origins=http://localhost:8081, or the browser and ADK's origin middleware will reject both the session REST request and the/run_liveWebSocket handshake.
WebSockets: The Key to Real-time Communicationβ
Standard HTTP (and SSE) cannot stream continuous microphone audio upstream from a browser while simultaneously streaming spoken audio downstream. WebSockets solve this by establishing a persistent, full-duplex (two-way) channel over a single TCP connection, enabling low-latency conversational voice with gemini-3.8-live.
The Data Flowβ
Figure: The 5-stage bidirectional streaming loop: microphone capture, WebSocket framing, ADK event dispatch, Gemini Live synthesis, and browser audio playback.
The interaction between a custom browser client and ADK's /run_live endpoint follows five stages:
- Create a Session via REST: Before connecting, send
POST /apps/{app_name}/users/{user_id}/sessionswith{"session_id": "..."}. ADK's/run_livehandler looks up the session by ID and closes the socket with code1002 ("Session not found")if it does not exist yet. - Await Readiness cleanly (No
setTimeoutRaces): Openws://localhost:8000/run_live?app_name={app_name}&user_id={user_id}&session_id={session_id}(which defaults tomodalities=["AUDIO"]), awaitmicAudioContext.audioWorklet.addModule(...), and await aPromisearound the WebSocketopenevent before starting microphone capture. - Client Streams 16kHz PCM Input:
- Use
navigator.mediaDevices.getUserMedia+AudioWorkletto capture raw 16-bit little-endian PCM at 16kHz (not compressedwebm/opusfromMediaRecorder). - Base64-encode each chunk and send a
LiveRequestJSON frame:{"blob": {"data": "<base64>", "mime_type": "audio/pcm;rate=16000"}}. - When the user releases push-to-talk, send
{"audio_stream_end": true}to flush cached server VAD audio.
- Use
- Server Streams 24kHz PCM
EventResponses:- ADK streams the audio to
gemini-3.8-liveover the Gemini Live API and forwards serialized ADKEventJSON frames back over the WebSocket. - Spoken audio arrives in
event.content.parts[]insideinlineData(mimeTypestarting with"audio/") as raw 16-bit PCM at 24kHz (encoded as RFC 4648base64url).
- ADK streams the audio to
- Gapless Client Playback:
- Normalize
base64url(-/_to+//), convert 16-bit PCM samples toFloat32, copy into a 24kHzAudioBuffer, and schedule eachAudioBufferSourceNodeatMath.max(playbackAudioContext.currentTime, nextPlayTime)so consecutive chunks play back-to-back without overlap or clicks.
- Normalize
A note on audio-first
/run_livesessions: By default, ADK's/run_liveendpoint configuresmodalities=["AUDIO"]for natural, low-latency spoken dialogue withgemini-3.8-live. In this lab, our client focuses strictly on capturing 16kHz microphone audio and playing back the 24kHz spoken audio stream.
When building a browser voice client against ADK's /run_live endpoint with gemini-3.8-live, why must you unlock playbackAudioContext.resume() inside the button click handler and await the WebSocket 'open' event?
Key Takeawaysβ
- Build custom voice clients by pairing
uv run adk api_server(runninggemini-3.8-live) with a browser WebSocket + Web Audio client. - Always create the session via
POST /apps/{app_name}/users/{user_id}/sessionsfirst, then openws://.../run_live?app_name=...&user_id=...&session_id=.... - Unlock
playbackAudioContext.resume()inside the user's click/press gesture so browser autoplay policies never silence incoming agent audio, and await explicit Promises foraudioWorklet.addModule()and WebSocketopenrather than relying onsetTimeoutdelays. - Gemini Live (
gemini-3.8-live) expects 16kHz 16-bit mono PCM input (audio/pcm;rate=16000) and outputs 24kHz 16-bit mono PCM audio chunks scheduled sequentially withnextPlayTime.
Closing Out Part 5: Ready for Enterprise AgentOps (Part 6)β
Congratulations on completing Part 5: Ecosystem Protocols & Multimodal Interfaces! Across Modules 27β30, you connected ADK agents to external MCP servers, generative A2UI surfaces, and low-latency duplex voice streams.
Next, in Part 6: Enterprise AgentOps with Google Agents CLI (Modules 30.1β30.4), you will graduate from standalone local ADK scripts to standardized enterprise repositories using google-agents-cliβstarting in Module 30.1 with Spec-Driven AgentOps, Coding-Agent Skills, and Prototype-First Scaffolding!