Make a Call
This guide covers how to make a call with the Vonage Client SDK. Before you begin, make sure you added the SDK to your app and (Android, iOS, JS).
NOTE: On iOS it is expected that you use CallKit for incoming and outgoing calls. Please follow the Outbound Calls Using iOS CallKit blog post.
Making a Call
The serverCall method allows you to initiate a voice call using the feature rich Voice API. When this method is called, the answer_url webhook that is configured for your Vonage Application will execute. That defines the logic and capabilities of the call.
// After creating a session
client.serverCall({to:phoneNumberOrUsername})
.then(callId => {
console.log("Id of created call: ", callId);
})
.catch(error => {
console.error("Error making call: ", error);
});
val client = VoiceClient(this.application.applicationContext)
client.serverCall(mapOf("to" to "PHONE_NUMBER")) {
err, outboundCall ->
when {
err != null -> {
// Handle call error.
}
else -> {
// Handle call success.
}
}
}
let client = VGVoiceClient()
client.serverCall(["to": "PHONE_NUMBER"]) { error, callID in
if error == nil {
// Handle call success.
} else {
// Handle call failure
}
}
Custom Data
Including your own custom data allows you to pass context, in a key-value format, to your answer_url webhook server. If you supply a value with the to key, it will populate the top level to in the query you make to your webhook server. Other data will be under the custom_data object.
// After creating a session
client.serverCall({to:phoneNumberOrUsername, device_name: "Alice app"})
.then(callId => {
console.log("Id of created call: ", callId);
})
.catch(error => {
console.error("Error making call: ", error);
});
client.serverCall(mapOf("to" to "PHONE_NUMBER", "device_name" to "Alice app")) {
err, outboundCall ->
...
}
client.serverCall(["to": "PHONE_NUMBER", "device_name": "Alice app"]) { error, callID in
...
}
The data will be available on the request's query made to your answer_url webhook server:
{
"to": "447000000000",
"from_user": "Alice",
"conversation_uuid": "CON-8dd32088-66be-42ae-b0af-c9e12ca588ed",
"uuid": "54c255ca-9c1c-4ecd-b175-a1d022dc7b07",
"custom_data": {
"to": "447000000000",
"device_name": "Alice app"
}
}
Listen For Call Events
To see updates on the state of a call, for example, to know if the other member answered or hung up the call, you should listen to leg status events.
To see updates on the status of the call:
// After creating a session
client.on("legStatusUpdate", (callId, legId, status) => {
console.log({callId, legId, status});
});
client.on("callInvite", (callId, from, channelType) => {
// Answer / Reject Call
console.log({callId, from, channelType});
});
client.on("callHangup", (callId, callQuality, reason) => {
console.log(`Call ${callId} has hung up, callQuality:${callQuality}, reason:${reason}`);
});
client.on("sessionError", (error) => {
console.error({error});
});
client.setOnLegStatusUpdate { callId, legId, status ->
// Call leg updates
}
client.setCallInviteListener { callId, from, channelType ->
// Handling incoming call invite
}
client.setOnCallHangupListener { callId, callQuality, reason ->
// Handle hangups
}
client.setSessionErrorListener { error ->
// Handle session errors
}
Add the current ViewController, or similar, as a delegate for the voice client:
client.delegate = self
ViewController will now have to conform to VGVoiceClientDelegate, the didReceiveLegStatusUpdateForCall function will update you on changes to call legs for the active call:
extension ViewController: VGVoiceClientDelegate {
func voiceClient(_ client: VGVoiceClient, didReceiveLegStatusUpdateForCall callId: String, withLegId legId: String, andStatus status: String) {
// Call leg updates
}
func voiceClient(_ client: VGVoiceClient, didReceiveHangupForCall callId: String, withQuality callQuality: VGRTCQuality, reason: VGHangupReason) {
// Handle hangups
}
func client(_ client: VGBaseClient, didReceiveSessionErrorWith reason: VGSessionErrorReason) {
// Handle session errors
}
}