Example: Websocket example, web client¶
This example shows how to use the Python client library to create a FastAPI-based web server that performs login to the Daisys API (so that credentials are kept secure) and interacts with the REST API to retrieve a websocket URL. This URL is passed to the front-end JavaScript application that makes the websocket connection and makes take requests, playing back the audio in a streaming fashion using the Web Audio API.
The included JavaScript shows how to:
Retrieve the URL using
websocket_url()
and open the websocket connection, automatically doing so again when disconnected, see websocket_connector.js.Define an async iterator that simplifies handling of streams for different requests by transforming the callback structure into a simple for loop, see websocket_stream.js and usage in websocket_client.js.
Send a request for generating a take, see websocket_client.js.
Use a single handler to handle incoming status messages and audio messages in both “parts” and “chunks” mode, see websocket_stream.js.
Respectively play the audio in a simple (parts, using audio sources) and more complex (chunks, using dynamic audio buffers) way using the Web Audio API, see part_audio_player.js and chunk_audio_player.js.
The above consists of several files, as opposed to other examples, so instead of repeating the example in the documentation, the reader is invited to follow the code in the git repository.
The application can be launched using:
python3 -m examples.websocket_client
or equivalently:
uvicorn examples.websocket_client:app