
Audio can be sent through WebSockets, but it requires a specific approach.
WebSockets provide a bi-directional, real-time communication channel between a client and a server, which makes them suitable for transmitting audio data.
To send audio through WebSockets, you need to encode the audio data into a format that can be transmitted over the WebSocket connection.
This can be done using codecs like Opus or Vorbis, which are designed for efficient audio compression and can handle real-time transmission.
What is Streaming?
Streaming is a real-time process that sends audio in chunks to a client, which plays it seamlessly. This is how online meetings allow people to hear each other in real time.
Audio streaming is used in various fields, including online meetings and podcasts. The audio is transmitted directly to the listener, making it a convenient way to access content.
In online meetings, audio is transmitted among all participants in real time. This allows for smooth communication and collaboration.
Curious to learn more? Check out: Streaming Services
Streaming Basics
Streaming Basics is a fundamental concept in the world of audio transmission. WebSockets, a technology that enables bidirectional, real-time communication between a client and a server, can be used to stream audio.
WebSockets establish a persistent connection between the client and server, allowing for efficient and continuous data transfer. This is especially useful for applications that require low-latency and high-bandwidth communication, such as live music streaming.
The WebSocket protocol uses the WebSocket URL to establish a connection, which is typically in the form of ws:// or wss://. This connection is then used to send and receive data, including audio streams.
Audio streaming can be achieved through WebSockets by sending audio data in small packets, called chunks, over the WebSocket connection. This allows for real-time transmission of audio data.
A different take: Find Streaming Access
Real-Time Streaming
Real-Time Streaming is a crucial aspect of audio transmission over WebSockets. By streaming audio in real-time, conversations feel natural and seamless, with low latency. This is achieved by leveraging WebSockets, which enable bi-directional, real-time communication between the client and server.
You might enjoy: Streaming Services Show Paranorman
Using WebSockets for real-time audio streaming is a game-changer for applications that require immediate feedback, such as live transcription services. With WebSockets, audio data can be transmitted in real-time, allowing for instant processing and transcription.
Robust error handling and performance optimization are essential for real-time audio processing over WebSocket connections. This involves implementing try-except blocks to handle connection loss or API failures, as well as using buffer systems to regulate data chunk flow and prevent server overload.
To ensure stability and responsiveness, timeouts and backpressure methods can be implemented within the asyncio event loop. This guarantees that audio is processed and transcribed without delays or data loss.
Real-time streaming also raises security concerns, particularly when handling sensitive data like speech. To address this, SSL/TLS encryption can be implemented for WebSocket connections, ensuring encrypted data streams between the server and client.
See what others are reading: Nextjs Ssr Websocket
Sending Audio via WebSocket
Sending Audio via WebSocket is indeed possible, and it's a great way to share audio in real-time. You can initiate a WebSocket connection from the scenario to send audio to a call.
On a similar theme: Nextjs 14 with Websocket
To accept an incoming WebSocket connection and receive audio, you need to prepare a WebSocket URL to access the current call and send the URL to the call. This is a crucial step that allows you to send audio to the call.
After you receive the URL, you can use it to send an audio to the call.
If this caught your attention, see: Facetime Audio Call
Send Text via WebSocket
To send text via WebSocket, use the WebSocket.send method. This method allows you to send text data to the server and receive it in real-time.
Sending text data via WebSocket is a straightforward process. You can use the WebSocket.send method to send text, just like you would send audio data.
The WebSocket.send method is a built-in method in most programming languages, making it easy to implement in your WebSocket application.
Intriguing read: Web Socket Api
WebSocket Endpoint
The WebSocket Endpoint is a crucial part of sending audio via WebSocket, and it's where real-time audio interaction is processed and streamed to the AI assistant. This is where the magic happens.
To establish a WebSocket connection, you need to accept the connection using `await websocket.accept()`. This ensures the connection is live and ready for communication.
The WebSocket connection is established when a client connects to `/media-stream`. This is where the audio interaction is processed and streamed to the AI assistant.
Here's a step-by-step breakdown of how the WebSocket Endpoint works:
- Accept the WebSocket Connection: The WebSocket connection is established when a client connects to `/media-stream` using `await websocket.accept()`, ensuring the connection is live and ready for communication.
The `/media-stream` WebSocket route is where real-time audio interaction is processed and streamed to the AI assistant.
Server Setup
To set up your server for sending audio through websockets, you'll need to ensure that your HTML structure is in order. Specifically, you'll need to have specific element IDs such as messageForm, message, messages, sendButton, and startAudioButton.
For the backend server, you'll need to have a Python FastAPI server running. This is a crucial step before you can start sending audio through websockets.
Here are the prerequisites for a smooth server setup:
- HTML Structure: messageForm, message, messages, sendButton, and startAudioButton element IDs
- Backend Server: Python FastAPI server running
- Audio Worklet Files: audio-player.js and audio-recorder.js for audio processing
Server Code
To send audio via WebSocket, use the call.sendMediaTo method. This method allows you to set a preferred encoding format and some custom parameters.
You can set a preferred encoding format, and some custom parameters when sending audio via WebSocket. If you don't set an encoding, PCM8 is selected by default.
Simplified Setup

Setting up your server is a breeze, thanks to the WebSocketAudioAdapter's simplified setup process. It requires no phone numbers and no external accounts, making it a plug-and-play solution.
With this adapter, you can skip the hassle of configuring telephony settings, and get straight to running your application. Just run the application with Uvicorn, and you'll see your application running in the logs.
Client-Agent Messaging
Client-Agent Messaging is a crucial aspect of any server setup, and it's essential to understand the different types of messaging that can occur between the client and the ADK agent.
There are two main types of messaging: agent-to-client messaging and client-to-agent messaging. Agent-to-client messaging streams ADK agent events to the WebSocket client, while client-to-agent messaging relays messages from the WebSocket client to the ADK agent.
In agent-to-client messaging, the ADK agent events are streamed to the client using WebSockets, and the logic involves iterating through live events, sending status flags, and extracting content. The content is then processed and sent to the client as JSON.

Client-to-agent messaging involves receiving and parsing JSON messages from the WebSocket client, expecting specific mime types and data formats. The messages are then sent to the ADK agent using the live_request_queue, either as text or audio input.
Here's a summary of the different message types and their corresponding actions:
To initiate a client-agent messaging session, the start_agent_session function is used, which creates a live session for the ADK agent, sets the response modality, and creates a queue for client inputs. The function returns a tuple containing the live events and the live request queue.
Prerequisites
To set up your server, you'll need to meet some basic prerequisites. Specifically, your HTML structure should include specific element IDs, such as messageForm, message, messages, sendButton, and startAudioButton.
Having a backend server running is also crucial. You'll need a Python FastAPI server up and running to make everything work smoothly.
You'll also need to have the necessary audio worklet files. These are audio-player.js and audio-recorder.js, which are used for audio processing.
Here are the specific prerequisites you'll need to meet:
- HTML Structure: messageForm, message, messages, sendButton, and startAudioButton element IDs.
- Backend Server: Python FastAPI server running.
- Audio Worklet Files: audio-player.js and audio-recorder.js.
Client-Agent Messaging
Client-Agent Messaging is a crucial aspect of WebSockets, enabling seamless communication between clients and agents. This is achieved through two primary functions: agent_to_client_messaging and client_to_agent_messaging.
The agent_to_client_messaging function streams ADK agent events to the WebSocket client in real-time. It iterates through live_events from the agent, sending status flags and content to the client.
The function extracts the first Part from event content and processes it accordingly. If the content is audio, it is Base64 encoded and sent as JSON with a mime type of "audio/pcm". If the content is partial text, it is sent as JSON with a mime type of "text/plain".
On the other hand, the client_to_agent_messaging function relays messages from the WebSocket client to the ADK agent. It receives and parses JSON messages from the client, expecting a specific format with mime types and data.
For text input, the function sends the content to the agent via live_request_queue.send_content(). For audio input, it decodes the Base64 data, wraps it in a Blob, and sends it via live_request_queue.send_realtime(). If the mime type is unsupported, it raises a ValueError.
In both cases, messages are logged for tracking purposes.
Prerequisites and Handling
To get started with sending audio through WebSockets, you'll need to ensure you have the right prerequisites in place. The HTML structure requires specific element IDs such as messageForm, message, messages, sendButton, and startAudioButton.
You'll also need a backend server running, specifically a Python FastAPI server. Additionally, you'll need to have the audio worklet files audio-player.js and audio-recorder.js for audio processing.
To handle audio, you'll use Audio Worklets, which are implemented via audio-player.js for playback and audio-recorder.js for capture. This involves storing AudioContexts and WorkletNodes, such as audioPlayerNode, in state variables.
Here's a quick rundown of the prerequisites and audio handling:
By having these prerequisites and understanding how to handle audio, you'll be well on your way to sending audio through WebSockets.
Client Side Flow
Client Side Flow works as follows:
The client establishes a WebSocket connection in text mode as soon as the page loads.
The user can then interact with the page by typing or submitting text, which is sent to the server for processing.
You might like: How Long Can Text Messages Be
The server responds with text, which is streamed back to the client for display.
Switching to audio mode is as simple as clicking the "Start Audio" button, which initializes audio worklets and sets the is_audio flag to true.
This also triggers a reconnect to the WebSocket in audio mode, enabling the client to send and receive audio data.
The client's audio recorder sends the mic audio as Base64 encoded PCM to the server for processing.
The server handles both audio and text responses, which are then displayed to the user through the WebSocket connection.
The WebSocket connection is also auto-reconnected if it closes for any reason, ensuring a seamless experience for the user.
How It Works
The WebSocketAudioAdapter is a powerful tool for real-time audio streaming. It uses WebSockets to establish a connection between the client (browser) and the server, allowing for seamless audio packet transmission.
This connection is established through the WebSocket Connection component, which is a crucial part of the adapter's functionality. The adapter sends audio packets in real-time through this connection, making it ideal for applications that require low-latency audio streaming.
Developers can easily set up endpoints for handling WebSockets traffic using Python's FastAPI framework. This integration allows for efficient management of WebSocket traffic and enables developers to focus on building their applications.
The audio adapter is powered by an AI-powered RealtimeAgent, which processes audio inputs and responds intelligently. This integration enables the adapter to provide advanced features such as real-time audio processing and analysis.
Here's a breakdown of the key components involved in the WebSocketAudioAdapter's functionality:
- WebSocket Connection: Establishes a connection between the client and server.
- Audio packets are streamed in real-time through this connection.
- Integration with FastAPI: Developers can easily set up endpoints for handling WebSockets traffic.
- Powered by Realtime Agents: The audio adapter integrates with an AI-powered RealtimeAgent.
The overall flow of the WebSocketAudioAdapter involves the client connecting to the server's websocket_endpoint, which accepts the connection and starts an ADK session. Two asyncio tasks manage communication, allowing for bidirectional streaming until disconnection or error.
Featured Images: pexels.com


