Twilio Project: Connecting to Twilio Movie using a web browser
Без кейворда
In this step we will write code to build that connects two people in movie conversation a web browser. Along the way we’ll review the classes that comprise the Twilio Movie API calls, and how they work together to give you plasticity to build custom-made, rich movie practice. Our workflow consists of several steps:
- Create an access token to initialize and activate your SDK’s connection to Twilio.
- Build an HTML template containing <div> tags for each participant’s movie.
- Create an endpoint for your local client and connect it to Twilio.
- Send an invite to another endpoint, which when accepted starts the movie call.
- Mange your movie conversation and fasten the movie to a div for each participant which has been invited.
You can attempt a demo of the application here . Here’s how to use the demo:
- Call a friend on another computer, or test locally using two browser windows.
- The demo assigns you a unique address name which emerges in the black text box.
- In the very first browser, inject the address of the other participant and click the call button.
- Accept the browser permissions dialog to use your movie camera.
- Movie Talk!
Access Tokens provide a secure way of granting each user of your movie application access to Twilio. Using a Twilio helper library, you can generate Access Tokens on your server and specify what capabilities you’d like a movie user to have. Your server provides the token to your application, which when passed to the javascript application enables Twilio’s movie features.
For demo purposes of this tutorial you can generate a demo Access Token in the Twilio Portal . The token will permit your application to access the Twilio Movie service for up to one hour.
Head over to the Twilio Portal to generate a demo access Token. Then, download the helper library in your language so you can generate access tokens on your server and integrate them into your application.
A good begin to any web application is the HTML template, so let’s begin building.
Very first, we’ll need to add a reference to twilio-conversations.js . This is is the JavaScript library Twilio provides for your web application to interface with Twilio Movie. To add it to your HTML page, simply add a <script> tag within the <head> of your document as goes after:
In this example the relative protocol is used, so the browser will automatically fountain the library over the page’s protocol (HTTP or HTTPS). Please make sure to reference the Twilio hosted version of the file, self-hosting of the JavaScript library is not supported.
This example also uses the jquery library for some convenience methods of accessing the HTML DOM. Jquery is not a requirement of Twilio Movie.
The next HTML elements required for our application are two <div> tags, one for your local movie stream and another for the remote movie you’ll be connecting with. Originally these elements won’t include any movie, so let’s add a placeholder photo to display when the page explosions. Later we’ll use Twilio Media objects (which wraps a WebRTC MediaStream) to append live movie flows to the <div> elements. Also, let’s include a label underneath each to identify who the movie belongs to. Here’s what it looks like:
The JavaScript library reference and placeholder divs for movie flows are the minimum required markup necessary to use Twilio movie! With these in place, your application can use the Twilio Movie JavaScript SDK to control and display movie.
If your application’s use case automatically connects movie without user interaction, you might be done. But for our project let’s add some extra UI to permit the user to come in a specific user’s name, to call them, and to hangup, as well as a <div> to output log messages. Here’s our final code for the html template. Please note the javascript which sets the access token near the bottom of the page. Your application should use a Twilio helper library to dynamically generate a value for the access token. The demo app uses a URL parameter to permit the name of the token to be specified per page.
Leave a Reply