メニュー

Expand
ページを評価:

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.

The Preflight API does not test users' bandwidth limitations. If you would like to test limitations in clients' available bandwidth, you should use the testMediaConnectionBitrate method from the RTC Diagnostics SDK.

Access the Preflight API

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 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 list of supported browsers here.

NPM

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');

Script tag

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;

CDN

You can also include the JavaScript SDK in your application from Twilio's CDN:

<script src="https://sdk.twilio.com/js/video/releases/2.20.0/twilio-video.min.js"></script>

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;

Use the Preflight API

Below is an example of how to use the Preflight API to start a diagnostic connectivity test and handle events during the test.

// import the Preflight API, which is called `runPreflight`,
// from the Twilio Video JavaScript SDK
const { runPreflight } = require('twilio-video');

// if you are using the Video JavaScript SDK via the Twilio CDN or
// a script tag, you would reference the Preflight API this way:
// const runPreflight = Twilio.Video.runPreflight;

// this assumes you have a function called getAccessToken
// to retrieve an Access Token from your server
const token = getAccessToken();

// run a preflight test, passing in an Access Token with
// a VideoGrant
const preflightTest = runPreflight(token);

// handle preflight test events

// while the test is in progress, the progress event fires
// whenever a particular PreflightProgress step completes
preflightTest.on('progress', (progress) => {
  console.log('preflight progress:', progress);
});

// if the test failed, the failed event fires and returns the error
// along with the partial test results it was able to collect
preflightTest.on('failed', (error, report) => {
  console.error('preflight error:', error);
  console.log('Received partial report:', report);
});

// if the test completed without error, the completed event fires
// and returns the preflight test report
preflightTest.on('completed', (report) => {
  console.log("Test completed in " + report.testTiming.duration + " milliseconds.");
  console.log(" It took " + report.networkTiming.connect?.duration + " milliseconds to connect");
  console.log(" It took " + report.networkTiming.media?.duration + " milliseconds to receive media");
});

Start the diagnostic test

To start a diagnostic test with the Preflight API, call the runPreflight() method and pass in an Access Token with a VideoGrant, which allows you to set up test connections to Twilio's servers.

You can optionally pass in other PreflightOptions when you run the test. The options include setting your preferred signaling region within the Twilio cloud (region; default is gll) and the amount of time to run the test (duration; the default is 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.

Listen for preflight test events

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.

Use PreflightProgress steps to track connectivity

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.

名前 概要
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

Review the completed test report

The completed event will pass back a report containing the results from the successful test. The report will contain the following fields:

名前 概要
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.

Stop an in-progress test

You can stop an in-progress test with the stop() method. This will stop the test and emit a failed event, along wtih partial test results that completed up to the point that the test stopped.

Video Diagnostics Application

Twilio offers an open-source Video Diagnostics Application that is built using the Prelight 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.

ページを評価:

ヘルプが必要ですか?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.

        
        
        

        フィードバックくださりありがとうございます!

        Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

        Sending your feedback...
        🎉 Thank you for your feedback!
        Something went wrong. Please try again.

        Thanks for your feedback!

        Refer us and get $10 in 3 simple steps!

        ステップ1

        Get link

        Get a free personal referral link here

        ステップ2:

        Give $10

        Your user signs up and upgrade using link

        ステップ3

        Get $10

        1,250 free SMSes
        OR 1,000 free voice mins
        OR 12,000 chats
        OR more