I'm using Azure Communication Service to create video calls between 2 people (person 1 and person 2).
The first person to connect (for the example person 1) waits for the second to connect (for the example person 2). When person 2 connects, they initiate the call. Person 1 is therefore notified and must answer the call using this function:
callAgent.on('incomingCall', async (args) => {
try {
//show the button accept call
} catch (error) {
}
});
Automatically if a person does not answer the call is unsuccessful.
My problem is that when the 2 people launch the call at the same time, one of the 2 answers but the other being already connected to the call cannot answer so the call is disconnected as if it had not answered.
Is it possible to destroy incomingCall when the call is started?
EDIT WITH sampath SOLUTION :
this code seems to work
async function incomingCallHandler(args) {
try {
incomingCall = args.incomingCall;
if(call.state === "Ringing" && call.direction === "Outgoing") {
// hangup outgoing call
await call.hangUp();
}
}
} catch (error) {
console.error(error);
}
};
callAgent.on('incomingCall', incomingCallHandler);
Steps for handling incoming calls and active calls simultaneously when both participants attempt to start a call at the same time:
incomingCall
event handler to listen for incoming calls. If the person is already on an active call, you may need to dismiss or decline the incoming call.args.call.reject()
within the incomingCall
event handler to decline the incoming call.incomingCall
and Managing in an Azure Communication.
let activeCall = null;
callAgent.on('incomingCall', async (args) => {
try {
// Check if there is an active call already
if (activeCall) {
// Decline the incoming call automatically if the user is in an active call
await args.call.reject();
console.log('Incoming call declined because the user is in an active call.');
} else {
// Show the button to accept the incoming call
// If accepted, set the call as activeCall
activeCall = await args.accept();
}
} catch (error) {
console.error('Error handling incoming call:', error);
}
});
// Sample code for initiating a call
async function initiateCall() {
if (activeCall) {
console.log('Already in a call, cannot initiate a new call.');
return;
}
// Assuming you have a target user ID or phone number to call
const targetUserIdentifier = { communicationUserId: '<USER_ID>' };
try {
activeCall = await callAgent.startCall([targetUserIdentifier]);
console.log('Call initiated successfully');
} catch (error) {
console.error('Error initiating call:', error);
}
}