Menu

Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Browser Dialer with Node.js and Vue.js

Download the Code

What Does This Thing Do?

This application allows you to make a phone call from your browser using Twilio Client.

It also includes a couple other standard features you would expect a browser phone to have:

  1. Mute and unmute microphone input
  2. Send DTMF touch tones using the HTML key pad

This tutorial highlights the key bits of code that make this application work. Check out the project README on GitHub to see how to run the code yourself.

Let's get started!

Creating a Twilio Application

This application requires a TwiML application to make a phone call, in this step we'll show you how to create it.

To create a TwiML App open you Console and click the button "Create new TwiML App".

Every TwiML application needs a friendly name (we called ours “Browser Dialer”) and either a Voice URL or a Messaging URL. Since we want our users to make phone calls, we used a URL to our application’s “/voice” route.

Note: When running the app locally, you can use a tool like ngrok to get a publicly accessible URL for your development environment.

Generate a capability token

Generate a Capability Token

Before our users can make any calls in their browsers, we need to create a capability token for them.

Capability tokens are your way to control exactly what your users can and can't do with Twilio Client. In this case, our server will provide all users with tokens that allow them to make outbound phone calls.

We use the Twilio Node Helper Library to generate and configure a capability token with the SID of our Twilio Application to allow outgoing calls.

Loading Code Sample...
      
      
      
      
      index.js

      The Twilio.js client needs credentials to be able to initiate calls as well as send messages, among other cool features. The Twilio.js client requests the token from the /token route, at which point the client is initialized and ready to start using Twilio API's. Next, let's set up the Twilio Device in the browser.

      Learn how to set up a twilio device

      Set Up the Twilio Device

      In our client-side code, we start by including the Twilio.js library.

      We then retrieve the capability token from the route /token with a POST request using jQuery.

      Lastly, we pass our token to Twilio.Device.setup() to finish the setup.

      The Twilio.Device.ready() callback is used to notify us when the device is ready to make calls.

      Loading Code Sample...
          
          
          
          
          public/dialer.js

          Set Up the Twilio Device

          public/dialer.js

          Setting up the Twilio Device was easy since the /token route simplifies authorization. Now that the Twilio Client is initialized, let's see how to start an outbound call.

          Learn how to make a call

          Make a Call

          Now that the Twilio Client is ready, our users can start making phone calls. They'll start by inputting the phone number they wish to call.

          We manage that input before passing the number on to Twilio, adding a + sign, then a country code, and the actual number. This is called the E.164 format and is required by most parts of Twilio's API.

          We then use Twilio.Device.connect() to start the call. Twilio will send a request to the URL you specified in your Twilio Application configuration, looking for instructions on how to handle the call.

          In this case, we include the phone number the user wishes to dial in our connect() call, and we then access it in our server-side code here.

          Loading Code Sample...
              
              
              
              
              public/dialer.js

              Call Toggle

              public/dialer.js

              As you have seen, connecting and disconnecting from a call is quite simple. Next, we'll enable the client to mute and unmute active calls.

              Mute or unmute an active call

              Mute and Unmute an Active Call

              Sometimes you want to mute or unmute the current call, halting input from the user's microphone.

              We can use Twilio.Device.activeConnection() to get the active call, and then call its mute() method to mute or unmute the user's microphone.

              Loading Code Sample...
                  
                  
                  
                  
                  public/dialer.js

                  The Mute Toggle

                  public/dialer.js

                  Now you have an idea of how simple it is to use the Twilio Client in the browser. There are much more features to explore. Next, let's allow the user to send DTMF tones, in case they are calling an automated phone system.

                  See how to work with DTMF tones

                  Send DTMF Tones

                  If our user calls an automated phone system, they might need to navigate a menu system using our phone's keypad and DTMF tones.

                  DTMF stands for "Dual-tone multi-frequency signaling" and are the familiar sounds you hear when dialing a phone. DTMF have been standardized so they can be understood and decoded by machines.

                  To play DTMF tones with the Twilio.js client library we use the sendDigits method, passing which digit the user pressed as our sole argument.

                  Loading Code Sample...
                      
                      
                      
                      
                      public/dialer.js

                      Digit Input Handler

                      public/dialer.js

                      As you have seen, sending DTMF tones in response to button clicks is straightforward. Lastly our clients may want to hang up a call. Let's see how we can do that next.

                      Learn how we hang up the call

                      Hang up the Call

                      Lastly, our users should be able to end a call.

                      To achieve this we use Twilio.Device.disconnectAll(), which terminates the current call.

                      Loading Code Sample...
                          
                          
                          
                          
                          public/dialer.js

                          Hang up Call

                          public/dialer.js

                          That's it! We crafted an application that allows our users to make browser-to-phone calls using Twilio.js.

                          Where to next?

                          Where to Next?

                          If you're a node developer working with Twilio, you might also enjoy these tutorials:

                          IVR: Phone Tree with Node.js and Express

                          Easily route callers to the right people and information with an IVR (interactive voice response) system.

                          Click to Call with Node.js and Express

                          Click-to-call enables your company to convert web traffic into phone calls with the click of a button.

                          Did this help?

                          Thanks for checking this tutorial out! If you have any feedback to share with us please contact us on Twitter, we'd love to hear it.

                          Kevin Whinnery Jose Oliveros David Prothero Kat King Hector Ortega Samuel Mendes Carlos Villavicencio

                          Need some help?

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

                          Loading Code Sample...