From 61bbfcb79bf261eda26978b8c4cc10e2aae3c999 Mon Sep 17 00:00:00 2001 From: Michael Hart Date: Sat, 1 Mar 2025 03:11:58 +1100 Subject: [PATCH] Add instructions for deploying with Cloudflare (#101) --- docs/deployment.md | 52 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/docs/deployment.md b/docs/deployment.md index 8bd6a9f..f6d991f 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -32,12 +32,12 @@ Stream( !!! warning This is a shared resource so we make no latency/availability guarantees. - For more robust options, see the Twilio and self-hosting options below. + For more robust options, see the Twilio, Cloudflare and self-hosting options below. ## Twilio API -The easiest way to do this is to use a service like Twilio. +An easy way to do this is to use a service like Twilio. Create a **free** [account](https://login.twilio.com/u/signup) and the install the `twilio` package with pip (`pip install twilio`). You can then connect from the WebRTC component like so: @@ -78,6 +78,52 @@ Stream( rtc_configuration = get_twilio_turn_credentials() ``` +## Cloudflare Calls API + +Cloudflare also offers a managed TURN server with [Cloudflare Calls](https://www.cloudflare.com/en-au/developer-platform/products/cloudflare-calls/). + +Create a **free** [account](https://developers.cloudflare.com/fundamentals/setup/account/create-account/) and head to the [Calls section in your dashboard](https://dash.cloudflare.com/?to=/:account/calls). + +Choose `Create -> TURN App`, give it a name (like `fastrtc-demo`), and then hit the Create button. + +Take note of the Turn Token ID (often exported as `TURN_KEY_ID`) and API Token (exported as `TURN_KEY_API_TOKEN`). + +You can then connect from the WebRTC component like so: + +```python +from fastrtc import Stream +import requests +import os + +turn_key_id = os.environ.get("TURN_KEY_ID") +turn_key_api_token = os.environ.get("TURN_KEY_API_TOKEN") +ttl = 86400 # Can modify TTL, here it's set to 24 hours + +response = requests.post( + f"https://rtc.live.cloudflare.com/v1/turn/keys/{turn_key_id}/credentials/generate", + headers={ + "Authorization": f"Bearer {turn_key_api_token}", + "Content-Type": "application/json", + }, + json={"ttl": ttl}, +) +if response.ok: + ice_servers = [response.json()["iceServers"]] +else: + raise Exception( + f"Failed to get TURN credentials: {response.status_code} {response.text}" + ) + +rtc_configuration = {"iceServers": ice_servers} + +stream = Stream( + handler=..., + rtc_configuration=rtc_configuration, + modality="audio", + mode="send-receive", +) +``` + ## Self Hosting We have developed a script that can automatically deploy a TURN server to Amazon Web Services (AWS). You can follow the instructions [here](https://github.com/freddyaboulton/turn-server-deploy) or this guide. @@ -171,4 +217,4 @@ Stream( modality="audio", mode="send-receive" ) -``` \ No newline at end of file +```