WebSocket transport implementation for FastAPI web applications
FastAPIWebsocketTransport
provides WebSocket support for FastAPI web applications, enabling real-time audio communication. It supports bidirectional audio streams and voice activity detection (VAD).
FastAPIWebsocketTransport is best suited for server-side applications and prototyping client/server apps.
For client/server production applications, we strongly recommend using a WebRTC-based transport for robust network and media handling.
To use FastAPIWebsocketTransport
, install the required dependencies:
FastAPI WebSocket connection instance
Transport configuration parameters
Add WAV header to audio frames
Frame serializer for WebSocket messages.
Common options include:
ExotelFrameSerializer
- For Exotel Websocket streaming integrationPlivoFrameSerializer
- For Plivo Websocket streaming integrationTelnyxFrameSerializer
- For Telnyx WebSocket streaming integrationTwilioFrameSerializer
- For Twilio Media Streams integrationSee the Frame Serializers documentation for more details.
Session timeout in seconds. If set, triggers timeout event when no activity is detected
Enable audio input from the WebRTC client
When enabled, incoming audio frames are pushed downstream
Enable audio output capabilities
Audio output sample rate in Hz
Number of audio output channels
Voice Activity Detection analyzer. You can set this to either
SileroVADAnalyzer()
or WebRTCVADAnalyzer()
. SileroVADAnalyzer is the
recommended option. Learn more about the
SileroVADAnalyzer.
Check out the Twilio Chatbot example to see how to use the FastAPI transport in a phone application.
FastAPIWebsocketTransport
provides callbacks for handling client connection events. Register callbacks using the @transport.event_handler()
decorator.
Called when a client connects to the WebSocket endpoint.
Parameters:
transport
: The FastAPIWebsocketTransport instanceclient
: FastAPI WebSocket connection objectCalled when a client disconnects from the WebSocket endpoint.
Parameters:
transport
: The FastAPIWebsocketTransport instanceclient
: FastAPI WebSocket connection objectCalled when a session times out (if session_timeout is configured).
Parameters:
transport
: The FastAPIWebsocketTransport instanceclient
: FastAPI WebSocket connection objectRaw audio data from the WebSocket client
Audio data to be sent to the WebSocket client
WebSocket transport implementation for FastAPI web applications
FastAPIWebsocketTransport
provides WebSocket support for FastAPI web applications, enabling real-time audio communication. It supports bidirectional audio streams and voice activity detection (VAD).
FastAPIWebsocketTransport is best suited for server-side applications and prototyping client/server apps.
For client/server production applications, we strongly recommend using a WebRTC-based transport for robust network and media handling.
To use FastAPIWebsocketTransport
, install the required dependencies:
FastAPI WebSocket connection instance
Transport configuration parameters
Add WAV header to audio frames
Frame serializer for WebSocket messages.
Common options include:
ExotelFrameSerializer
- For Exotel Websocket streaming integrationPlivoFrameSerializer
- For Plivo Websocket streaming integrationTelnyxFrameSerializer
- For Telnyx WebSocket streaming integrationTwilioFrameSerializer
- For Twilio Media Streams integrationSee the Frame Serializers documentation for more details.
Session timeout in seconds. If set, triggers timeout event when no activity is detected
Enable audio input from the WebRTC client
When enabled, incoming audio frames are pushed downstream
Enable audio output capabilities
Audio output sample rate in Hz
Number of audio output channels
Voice Activity Detection analyzer. You can set this to either
SileroVADAnalyzer()
or WebRTCVADAnalyzer()
. SileroVADAnalyzer is the
recommended option. Learn more about the
SileroVADAnalyzer.
Check out the Twilio Chatbot example to see how to use the FastAPI transport in a phone application.
FastAPIWebsocketTransport
provides callbacks for handling client connection events. Register callbacks using the @transport.event_handler()
decorator.
Called when a client connects to the WebSocket endpoint.
Parameters:
transport
: The FastAPIWebsocketTransport instanceclient
: FastAPI WebSocket connection objectCalled when a client disconnects from the WebSocket endpoint.
Parameters:
transport
: The FastAPIWebsocketTransport instanceclient
: FastAPI WebSocket connection objectCalled when a session times out (if session_timeout is configured).
Parameters:
transport
: The FastAPIWebsocketTransport instanceclient
: FastAPI WebSocket connection objectRaw audio data from the WebSocket client
Audio data to be sent to the WebSocket client