Skip to main content
Building with a coding agent? The coding agent version of this guide gives you the prompts, what to check, and what to tell your agent when it goes wrong.

What you’ll build

A browser page that captures microphone audio, streams it to Dialog-RSN-1 over a WebSocket, and renders the reply as text while it’s still arriving. You’ll use getUserMedia for the microphone, AudioWorklet to reach the raw samples, and a WebSocket for the wire protocol. There’s no SDK to install and no build step.
Text replies only. This guide shows the reply on the page, so it asks for text with output_modalities: ["text"] and stops there. Set the field explicitly: a session that leaves it out speaks its replies, and a page listening for text deltas then shows nothing. For a spoken reply, leave the default and play response.output_audio.delta (base64 PCM16 at 24 kHz) through Web Audio, or stream the text to your own text-to-speech.

Connecting from the browser

Your API key from studio.poly.ai is a bearer credential, and anyone visiting your page can open dev tools and read it out of your JavaScript. Don’t connect from the browser to wss://api.studio.poly.ai/v1/realtime with the key embedded in client code. Put a small relay in front of it: a same-origin WebSocket endpoint on your own server that holds the real key, opens the upstream connection to Dialog-RSN-1, and pipes frames through in both directions. The browser talks to your relay and nothing else.
connect
The companion repo includes a Node relay, relay.js, that also serves the page. If you write your own, set a User-Agent on the upstream connection. The API refuses a handshake without one with a bare 403, and Node’s ws package sends none by default. The code below assumes that relay exists and connects to it:
connect.js

Capturing microphone audio

The browser gives you microphone audio as floating-point samples, and Dialog-RSN-1 wants base64-encoded 16-bit PCM. Do that conversion in an AudioWorkletProcessor, which runs off the main thread, so encoding audio doesn’t compete with rendering the page.
pcm-worklet.js
capture.js
The browser takes the AudioContext sample rate from the hardware, 48 kHz on most machines, where Dialog-RSN-1’s native rate is 16 kHz. Declare your true rate with the poly_input_rate extension field on session.update, and the server resamples on the way in:
session.update
Send the rate your AudioContext reports in audioContext.sampleRate. Hardcoding 48000 is wrong on hardware that runs at 44.1 kHz: the server then resamples from a rate your audio was never at, and hears it about 9% fast. You can read the rate before the user clicks anything. A context created on page load starts suspended, but its sampleRate is already set. Call audioContext.resume() first thing in the click handler, before you wait on the microphone: Safari only lets a context start during the click itself. The full example below does this.

Streaming audio and handling server VAD

Once you’ve wired up capture, input_audio_buffer.append frames flow without a break. The server decides when the caller starts and stops talking, and tells you with these events:
vad.js
speech_stopped arrives once Dialog-RSN-1 judges that the caller has finished. A speech_started can arrive with no speech_stopped after it, when the sound turns out not to be the caller. While a reply is playing, speech_started waits until Dialog-RSN-1 confirms the caller is talking. There’s no client-side VAD to configure or tune. If the caller starts talking while a response is still streaming, Dialog-RSN-1 confirms the audio came from the caller and then cancels the response for you. You get the same response.done with status: "cancelled" that response.cancel produces, with status_details.reason set to "turn_detected" rather than "client_cancelled".

Reconstructing the streamed reply

The reply arrives as a series of small events. Accumulate response.output_text.delta by the item_id it belongs to, and treat response.done as the signal the turn is over:
reply.js
Keying by item_id keeps each reply separate. Only one response runs at a time and each carries one text item, so deltas never interleave, but a new reply’s deltas start a new entry instead of appending to the last one. Keep the error branch. Every failure on this API arrives as an error event and leaves the connection open, as the quickstart explains. Without the branch, a rejected session.update looks like a page that connects and never answers.

Full example

Three files: the page, its script, and the pcm-worklet.js from Capturing microphone audio saved next to them.

Full code on GitHub

These files plus the relay, ready to clone and run with npm start.
index.html
app.js

Next steps

Overview

What Dialog-RSN-1 does well, with links to each feature.

API reference

Every event, the session object, function calling, and error codes.

Python from scratch

The same protocol, built server-side with plain websockets.

Your own text-to-speech

Speak the streamed reply with your own voice: barge-in, playback reports, and time to first audio.

Realtime agents sample

A larger browser app on the same relay pattern: three one-agent scenarios with searched knowledge, a flow step and a language switch.
Last modified on November 24, 2026