JavaScript
Create a client side application
Create an HTML file called client_js.html. Add the following code, but make sure you paste the JWT you generated for the user in the earlier step:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="./node_modules/@vonage/client-sdk/dist/vonageClientSDK.min.js"></script>
<style>
input, button {
font-size: 1rem;
}
#call, #hangup {
display: none;
}
</style>
</head>
<body>
<h1>Call Phone from App</h1>
<label for="phone-number">Your Phone Number:</label>
<input type="text" name="phone-number" value="" placeholder="i.e. 14155550100" id="phone-number" size="30">
<button type="button" id="call">Call</button>
<button type="button" id="hangup">Hang Up</button>
<div id="status"></div>
<script>
const callButton = document.getElementById("call");
const hangUpButton = document.getElementById("hangup");
const statusElement = document.getElementById("status");
const token = 'ALICE_JWT';
const client = new vonageClientSDK.VonageClient();
let callId = null;
client.createSession(token)
.then((sessionId) => {
console.log("Id of created session: ", sessionId);
callButton.style.display = "inline";
})
.catch((error) => {
console.error("Error creating session: ", error);
});
client.on('legStatusUpdate', (callId, legId, status) => {
if (status === "ANSWERED") {
callButton.style.display = "none";
hangUpButton.style.display = "inline";
}
if (status === "COMPLETED") {
callId = null;
callButton.style.display = "inline";
hangUpButton.style.display = "none";
}
});
callButton.addEventListener("click", event => {
const destination = document.getElementById("phone-number").value;
if (destination !== "") {
client.serverCall({ to: destination })
.then((_callId) => {
callId = _callId;
})
.catch((error)=>{
console.error(`Error making call: ${error}`);
});
} else {
statusElement.innerText = 'Please enter your phone number.';
}
});
hangUpButton.addEventListener("click", () => {
client.hangup(callId)
.then(() => {
hangUpButton.style.display = "none";
callButton.style.display = "inline";
})
.catch(error => {
console.error("Error hanging up call: ", error);
});
});
</script>
</body>
</html>
This is your web application that uses the Client SDK to make an outbound call to a phone.
The main features of this code are:
- A button to call.
- A button to hang up.
- The code creates a session using the JWT generated earlier.
- Then code sets up two event listeners for the call and hang up buttons.
- The first listener will use the
serverCallfunction to make the outbound call. Your webhook server will receive the destination phone number in the request body and route the call. - The second listener will call
hangupto end the call.
- The first listener will use the
- Another event handler is set for leg status updates from the callee. Once they answer the hang up button will be shown and the call button hidden, and when the call ends this is reverted.
Making an in-app voice call
Make a voice call from a web app to a phone using the JavaScript Client SDK.
Steps
1
Introduction2
Prerequisites3
Create a webhook server4
Create a Vonage Application5
Link a Vonage number6
Create a User7
Generate a JWT8
Install Client SDK9
Create a client side application10
Run your application11
What's next?