Preflight API
The Twilio Video JavaScript Preflight API provides functions for testing connectivity to the Twilio Cloud. The API can identify signaling and media connectivity issues and provide a report at the end of the test. You can use the Preflight API in your Twilio Video applications to detect issues before a Participant joins a Video Room or as part of a troubleshooting page.
To check connectivity, the Preflight API creates two peer connections from the local user to Twilio's Signaling and TURN servers. It publishes synthetic audio and video tracks from one of those connections and ensures that the other connection receives the media on those tracks. After successfully verifying connectivity, it generates a report with information about the connection.
Info
The Preflight API doesn't test user bandwidth limitations. If you want to test client bandwidth limitations, use the testMediaConnectionBitrate method from the RTC Diagnostics SDK.
The Preflight API is included with the Twilio Video JavaScript SDK in versions 2.16.0 and above. Versions 2.16.0 through 2.19.1 of the JavaScript SDK contain a beta version of the Preflight API. Version 2.20.0 and above of the JavaScript SDK contain the generally available Preflight API, which is no longer in beta.
You can include the JavaScript SDK in your application either by installing it with Node Package Manager (npm) or using the Twilio CDN.
See the supported browsers.
Install the Video JavaScript SDK using npm:
npm install --save twilio-video
Then, you can start using the Preflight API in your application (note that the Preflight API is under the name runPreflight):
const { runPreflight } = require('twilio-video');
You can also copy twilio-video.min.js from the twilio-video/dist folder after npm installing it and include it directly in your web app using a <script> tag.
<script src="https://my-server-path/twilio-video.min.js"></script>
Using this method, you can access the PreflightTest API like so:
const runPreflight = Twilio.Video.runPreflight;
You can also include the JavaScript SDK in your application from Twilio's CDN:
<script src="https://sdk.twilio.com/js/video/releases/2.34.0/twilio-video.min.js"></script>
Info
You should make sure you're using the latest Twilio Video JavaScript SDK release. To find the CDN link for the most recent JavaScript SDK release, visit the JavaScript SDK latest release documentation.
Using the CDN, the JavaScript SDK will set a browser global that you can use to reference the Preflight API:
const runPreflight = Twilio.Video.runPreflight;
The following example shows how to use the Preflight API to start a diagnostic connectivity test and handle events during the test.
1// import the Preflight API, which is called `runPreflight`,2// from the Twilio Video JavaScript SDK3const { runPreflight } = require('twilio-video');45// if you are using the Video JavaScript SDK via the Twilio CDN or6// a script tag, you would reference the Preflight API this way:7// const runPreflight = Twilio.Video.runPreflight;89// this assumes you have a function called getAccessToken10// to retrieve an Access Token from your server11const token = getAccessToken();1213// run a preflight test, passing in an Access Token with14// a VideoGrant15const preflightTest = runPreflight(token);1617// handle preflight test events1819// while the test is in progress, the progress event fires20// whenever a particular PreflightProgress step completes21preflightTest.on('progress', (progress) => {22console.log('preflight progress:', progress);23});2425// if the test failed, the failed event fires and returns the error26// along with the partial test results it was able to collect27preflightTest.on('failed', (error, report) => {28console.error('preflight error:', error);29console.log('Received partial report:', report);30});3132// if the test completed without error, the completed event fires33// and returns the preflight test report34preflightTest.on('completed', (report) => {35console.log("Test completed in " + report.testTiming.duration + " milliseconds.");36console.log(" It took " + report.networkTiming.connect?.duration + " milliseconds to connect");37console.log(" It took " + report.networkTiming.media?.duration + " milliseconds to receive media");38});
To run a diagnostic test with the Preflight API, call the runPreflight() method and pass in an Access Token with a VideoGrant. This allows you to set up test connections to Twilio's servers.
When you run the test, you can pass in other PreflightOptions. The options include setting your preferred signaling region within the Twilio cloud (default: gll, which connects to the nearest signaling server based on latency) and the amount of time, duration, to run the test (default: 10000 ms, or 10 seconds).
The Access Token you pass to the runPreflight must contain a VideoGrant.
Because the Preflight API is connecting to a test Video Room that the Preflight API sets up, any value you pass in for the room and identity fields in the VideoGrant will be ignored during the preflight test. The Access Token you use does require an identity field, but this identity can be any value for the purpose of the test.
After you start running the preflight test with the runPreflight method, the test can generate three types of events. The code example above demonstrates how to listen for each event type.
progress: The test is in progress and a PreflightProgress step in the test completed. This event passes the specific PreflightProgress step that completed.failed: The test failed with an error. This event passes back the error and any partially generated test results.completed: The test completed successfully and you can review the results. This event passes back the completed test report.
While the Preflight Test is in progress, it will emit ProgressEvents indicating it completed specific connectivity checks. You can use these events to track the test progress and provide additional feedback to end users about their connections.
| Name | Description |
|---|---|
| mediaAcquired | Successfully generated synthetic tracks |
| connected | Successfully connected to Twilio's server and obtained TURN credentials |
| mediaSubscribed | The test connection successfully subscribed to media tracks |
| mediaStarted | Media flow was detected |
| dtlsConnected | Established DTLS connection. This event will be not be emitted on Safari browsers. |
| peerConnectionConnected | Established a PeerConnection. This event will not be emitted on Firefox browsers. |
| iceConnected | Established an ICE connection |
The completed event will pass back a report containing the results from the successful test. The report will contain the following fields:
| Name | Description |
|---|---|
| testTiming | Time measurements for when the test started and ended (in Epoch time) and how long the test lasted in ms. |
| networkTiming | Networking timing measurements captured during the test. |
| iceCandidateStats | An array containing the gathered ICE clients for STUN/TURN. Learn more about STUN, TURN, and ICE here. |
| selectedIceCandidatePairStats | Information about the ICE candidates that were used for the connection, such as the IP address, port, and protocol used. |
| progressEvents | A list of the ProgressEvents that occurred during the test. |
| stats | RTC-related statistics captured during the test. Contains information about the average, minimum, and maximum jitter, round trip time (rtt), and packet loss during the test. |
You can stop an in-progress test with the stop() method. This will stop the test and emit a failed event along with partial test results that completed up to the point that the test stopped.
The preflight test answers two questions: "Can this device connect to Twilio?" and "What will the call quality be like?" This section explains how to interpret the test results to answer both.
Use the combination of progress events and the failed event to determine connectivity:
| Outcome | What it means | Error code/message | Recommended action |
|---|---|---|---|
All progress events fire, completed emits | The device can reach a Twilio TURN relay | N/A | Connection is viable |
failed before mediaAcquired | Cannot generate synthetic tracks | N/A | Browser API issue (not a network problem) |
failed after mediaAcquired but before connected | Cannot reach the Twilio signaling server | Code 53000: "Signaling connection error" | Check firewall or proxy rules for WSS on port 443 |
failed after connected but before mediaStarted | TURN credentials acquired but media path blocked | Code 53405: "Media connection failed or Media activity ceased" | Check UDP/TCP relay ports; may need TURN-TCP fallback |
Info
The mediaAcquired event does not access the user's camera or microphone. It generates synthetic audio/video tracks locally. This is a browser capability check, not a network check. The first network-dependent step is connected, which establishes a WebSocket connection to the signaling server and acquires TURN credentials.
The preflight test forces iceTransportPolicy: 'relay' on the publisher connection, meaning it only tests the Twilio TURN relay path. If the preflight succeeds, the real call (which can also use direct or STUN paths) also works. If the preflight fails, your network blocks relay traffic entirely.
The networkTiming field provides duration measurements for each connection phase. The connect.duration value reflects multiple sequential round-trips (WebSocket handshake and TURN credential exchange), not a single RTT measurement. An expected baseline is 800-1000 ms for connect.duration.
Use these signals to identify specific problems:
connect.duration> 5000 ms: Signaling server latency is high, possibly a distant region. Consider specifying a closer region.media.duration> 10000 ms: Media path is degraded, even if the connection succeeds.ice.duration> 5000 ms: ICE connectivity check is slow, possible symmetric NAT or firewall interference.
The stats field in the completed report contains three metrics you can use to estimate call quality.
Warning
Jitter is reported in seconds (per the WebRTC inbound-rtp specification), not milliseconds. A value of 0.015 means 15 milliseconds. RTT is reported in milliseconds and packet loss is reported as a percentage (0-100).
Use the following thresholds to interpret the stats:
| Metric | Good | Acceptable | Poor | Unit in report |
|---|---|---|---|---|
| RTT (round-trip time) | < 100 | 100-300 | > 300 | milliseconds |
| Jitter | < 0.030 | 0.030-0.100 | > 0.100 | seconds |
| Packet loss | < 1 | 1-5 | > 5 | percent |
The following reference implementation shows how to derive a quality estimate from the report:
1function estimateQuality(report) {2const { rtt, jitter, packetLoss } = report.stats;3if (!rtt || !jitter || !packetLoss) return 'insufficient-data';45// Use average values for overall assessment6const rttMs = rtt.average; // already in milliseconds7const jitterSec = jitter.average; // in seconds per WebRTC spec8const lossPercent = packetLoss.average;910if (rttMs < 100 && jitterSec < 0.030 && lossPercent < 1) return 'excellent';11if (rttMs < 200 && jitterSec < 0.050 && lossPercent < 3) return 'good';12if (rttMs < 300 && jitterSec < 0.100 && lossPercent < 5) return 'acceptable';13return 'poor';14}
The selectedIceCandidatePairStats field shows how the connection was established. Because the preflight test forces iceTransportPolicy: 'relay' on the publisher connection, the selected pair always uses a relay candidate. The relayProtocol field indicates how constrained your network is:
| Local candidate | Relay protocol | What it means |
|---|---|---|
candidateType: "relay" | udp | TURN-UDP relay. Direct UDP connection to the Twilio TURN server. Allows for low-latency video and audio. |
candidateType: "relay" | tcp | TURN-TCP relay. UDP ports are blocked on this network. Expect higher latency and suboptimal quality. |
candidateType: "relay" | tls | TURN-TLS relay. This is the most restrictive network path because UDP and plain TCP are blocked. TLS overhead adds the highest latency. |
The relay protocol is a proxy for how much overhead the connection carries. If you are on TURN-TLS, you generally experience worse quality than on TURN-UDP, all else being equal.
1// report.stats shows:2// rtt: { average: 450, min: 200, max: 800 }3// jitter: { average: 0.085, min: 0.020, max: 0.150 }4// packetLoss: { average: 8.2, min: 0, max: 15 }
High RTT suggests the user is far from the nearest Twilio media region, or their network has congestion. High packet loss causes visible artifacts. Try specifying a closer region in Room options, or advise the user to switch to a wired connection.
1// selectedIceCandidatePairStats.localCandidate shows:2// candidateType: "relay", relayProtocol: "tls"
The network blocks UDP and plain TCP, forcing the most restrictive relay path. Video works, but exclusively through TURN-TLS relay, which adds latency and results in degraded call experience. This can happen in corporate networks with strict firewalls where the Twilio media IP blocks and ports are not allowlisted. See Video IP addresses and firewall rules for the required network configuration.
1// progressEvents shows: mediaAcquired, connected, mediaSubscribed, but no mediaStarted2// error: "Media connection failed or Media activity ceased"
The TURN relay connected but the media path failed. This may indicate deep packet inspection interfering with encrypted traffic, or UDP being blocked entirely.
Use this flowchart to decide what to tell your users based on the preflight result:
1Run preflight test2|3v4completed? ──No──> Check error + progressEvents5| |6Yes Which stage failed?7| |8v [See connectivity table]9Check stats10|11v12All "Good"? ──Yes──> "Ready for Video"13|14No15|16v17Which metric is poor?18|19┌────┼────────┐20v v v21RTT Jitter Packet Loss22| | |23v v v24Suggest Suggest Suggest25closer wired wired or26region connection less congested27network
The preflight test has the following limitations:
- No bandwidth testing: The preflight test only tests connectivity and path quality. It does not measure available bandwidth. Use the RTC Diagnostics SDK
testMediaConnectionBitratemethod for bandwidth testing. - Synthetic media only: The test does not access the user's real camera or microphone. It uses synthetic audio and video tracks to verify the media path.
- Single pub/sub pair: The test does not replicate the exact topology of a multi-party room. It tests a single publish/subscribe pair through the Twilio TURN server.
- TURN-relay only: The test uses
iceTransportPolicy: 'relay', so stats reflect the relay path. In a real call, if a direct connection is available, quality may be better than what the preflight reports. - Test duration: The default duration is 10 seconds. The test collects stats once per second, yielding approximately 9 data points. Longer durations (configurable through
options.durationin milliseconds) produce more samples and are better at detecting intermittent degradation, but increase user wait time.
Twilio offers an open-source Video Diagnostics Application that is built using the Preflight API and the RTC Diagnostics SDK. You can deploy and explore this application to see the different functionality of these diagnostic APIs in action. The application tests participants' device and software setup, connectivity with the Twilio Cloud, and network performance. It provides users feedback about their network quality and device setup, and also includes recommendations for improving their video call quality.