Make a call to a web browser

How to receive calls into your web app

During this guide you will learn how to:

  • Setup the WebRTC JS SDK into your app.
  • Use the necessary SDK APIs to connect and receive calls into your web application.

Requirements

Building the app

  1. Add the WebRTC adapter as a script
   <script
     type="text/javascript"
     src="https://webrtc.github.io/adapter/adapter-latest.js">
   </script>
  1. Add the Telnyx RTC SDK as a script
   <script
     type="text/javascript"
     src="https://unpkg.com/@telnyx/webrtc">
   </script>
  1. Initialize the client using your SIP credentials
  var client = new TelnyxWebRTC.TelnyxRTC({
         login: “SIP USER”,
         Password: “SIP USER PASSWORD”,
  });
  1. Attach the event listeners to receive updates from the SDK
  client
       .on('telnyx.ready', () => console.log('ready to call'))
       .on('telnyx.notification', (notification) => {
         console.log('notification:', notification)
  });
  1. Inside the telnyx.notification event you can detect the incoming call and answer it. On this example we are anwering it as soon as we get the incoming call notification
  client.on('telnyx.notification', (notification) => {
      const call = notification.call;

      // Check the type of the notification
      if (notification.type === 'callUpdate' && call.state === 'ringing') {
          // Answer the call as soon as the notification is received.
          call.answer();
      }
  });
  1. Enable the microphone
client.enableMicrophone();
  1. Connect the client
client.connect();
  1. Now your application is capable of receiving voice calls.

How to test it

  1. Go to https://webrtc.telnyx.com/
  2. Login with SIP credentials (caller SIP credentials). At this point you should have created 2 SIP connections one for the caller and another for the callee.

    Telnyx-webrtc-register

  3. Enter the destination SIP username (callee SIP connection) and press the call button.

    Telnyx-webrtc-call

  4. The telnyx.notification listener will be fired and the call will automatically be answered.

Test the vanilla example

Was this page helpful?