Skip to content
Last updated

Voice calling

Voice calling with javascript Voice and Video SDK. Set up calling between applications, from application to phone, app to SIP and conference calls. Get more information here.

The Sinch SDK supports four types of calls: app-to-app (audio or video), app-to-phone, app-to-sip and conference calls. The CallClient is the entry point for the calling functionality of the Sinch SDK.

Calls are placed through the CallClient and events are received using the CallClientListener. The call client is owned by the SinchClient and accessed using sinchClient.callClient.

Set up an App-to-App call

Use the CallClient.callUser() method so Sinch can connect the call to the callee.

var callClient = sinchClient.callClient;
var call = callClient.callUser('<remote user id>');
// Video call:
// var call = callClient.callUserVideo('<remote user id>');
call.addListener(...);

The returned call object includes participant details, start time, state, and possible errors.

Assuming the callee’s device is available, CallListener.onCallProgressing() is invoked. If you play a progress tone, start it here. When the callee receives the call, CallListener.onCallRinging() fires. This indicates the callee’s device is ringing. When the other party answers, CallListener.onCallAnswered() is called. Stop any progress tone. Once full audio connectivity is established, CallListener.onCallEstablished() is emitted. Users can now talk.

Typically, connectivity is already established when the call is answered, so onCallEstablished may follow immediately after onCallAnswered. On poor networks, it can take longer—consider showing a “connecting” indicator.

Set up an App-to-Phone call

An app-to-phone call is a call that's made to a phone on the regular telephone network. Setting up an app-to-phone call is not much different than setting up an app-to-app call. Instead of invoking the callUser method, invoke the CallClient.callPhoneNumber() method and pass an E.164 number prefixed with +. For example, to call the US phone number 415 555 0101, specify +14155550101. The + is the required prefix with the US country code 1 prepended to the local subscriber number.

Presenting a number to the destination you are calling

Mandatory step!

You must provide a CLI (Calling Line Identifier) or your call will fail.

You need a number from Sinch so you can provide a valid CLI to the handset you are calling. If you have a trial account, you can use the test number that is available for free. If you have already upgraded your account, you can purchase a number to use on the dashboard.

Specify your CLI during building SinchClient by using callerIdentifier method.

Note:

When your account is in trial mode, you can only call your verified numbers. If you want to call any number, you need to upgrade your account!

Credits Required

Remember, placing an App-to-Phone call requires that your Sinch account has sufficient credits. Add credits on your Account page. Credits are used each time an App-to-Phone call is placed and your account balance is updated after each call.

Testing DTMF

In App-to-Phone scenario, it's possible to issue DTMF sequences using Sinch SDK. Note that if the receiving end of the call is an iOS device, you might have to disable VoLTE ("Voice over LTE") option in the settings of the phone at the receiving end of the call in order to be able to hear DTMF tones.

Set up an App-to-sip call

An app-to-sip call is a call that's made to a SIP server. Setting up an app-to-sip call is not much different from setting up an app-to-app call. Instead of invoking the callUser method, invoke CallClient.callSip(). The SIP identity should be in the form user@server. By convention, when passing custom headers in the SIP call, the headers should be prefixed with x-. If the SIP server reports any errors, the CallDetails object will provide an error with the SIP error type.

Set up a Conference call

A conference call can be made to connect a user to a conference room where multiple users can be connected at the same time. The identifier for a conference room may not be longer than 64 characters.

var callClient = sinchClient.callClient;
var call = callClient.callConference('<conferenceId>');
call.addCallListener(...);

It is also possible to connect users to a conference call via the Sinch REST API.

Selecting audio input device

To select a specific audio input device, set the deviceId constraint via CallClient.setAudioTrackConstraints().

sinchClient.callClient.setAudioTrackConstraints({
  deviceId: { exact: <yourVideoDeviceId> },
});

For a full sample of input/output device selection, see the reference app.

Handle incoming calls

To answer calls, the application must be notified when the user receives an incoming call.

Add a CallClientListener to CallClient to act on incoming calls. When a call arrives, CallClientListener.onIncomingCall() is executed.

var callClient = sinchClient.callClient;
callClient.addCallClientListener(...);

When the incoming call method is executed, the call can either be connected automatically without any user action, or it can wait for the user to press the answer or the hangup button. If the call is set up to wait for a user response, we recommended that a ringtone is played to notify the user that there is an incoming call.

onIncomingCall = (callClient, call) => {
    // Start playing ringing tone
    ...

    // Add call listener
    call.addCallListener(...);
}

To get events related to the call, add a call listener. The call object contains details about participants, start time, potential error codes, and error messages.

Incoming video call

When an incoming call is a video call, the CallClientListener.onIncomingCall() section for details on how to add video views.

Answer incoming call

To answer the call, use the Call.answer() method on the call to accept it. If a ringtone was previously played, it should be stopped now.

User presses the answer button:

// User answers the call
call.answer();

// Stop playing ringing tone

Decline incoming call

If the call shouldn't be answered, use Call.hangup() on the call to decline. The caller is notified that the incoming call was denied. If a ringtone was previously played, it should be stopped now.

User presses the hangup button:

// User doesn't want to answer
call.hangup();

// Stop playing ringing tone
...

Disconnecting a Call

When the user wants to disconnect an ongoing call, use Call.hangup() method. Either user taking part in a call can disconnect it.

Hanging up a call:

call.hangup();

When either party disconnects a call, the application is notified using the call listener method CallListener.onCallEnded(). This allows the user interface to be updated, an alert tone to be played, or similar actions to occur.

A call can be disconnected before it has been completely established.

Hanging up a connecting call:

// Starts a call asynchronously
var call = callClient.callUser('<remote user id>');

// User changed his/her mind, let’s hangup
call.hangup();