Skip to main content
Version: v4

Default Call

This section will provide information on how a complete calling workflow can be set up using the CometChat SDK. We've built the complete workflow to help your users make calls, receive calls as well as accept/reject calls.

Let us assume Alex to be the call initiator and Bob is the receiver.

  1. Alex initiates the call to Bob using the initiateCall() method.
  2. Bob now has two choices:
  • Accept the call from Alex using the acceptCall() method.
  • Reject the call from Alex using the rejectCall() method passing the status as rejected.
  1. In the meantime, Alex has the option to cancel the call he initiated to Bob using the rejectCall() method passing the status as cancelled.
  2. If Bob accepts the call from Alex, both Alex and Bob need to call the startSession() method. Alex in the onOutgoingCallAccepted() method of the CallListener and Bob in the success obtained from the acceptCall() method and both will be connected to each other.

Initiate Call

The initiateCall() method sends a call request to a user or a group.

var receiverID = "UID";
var callType = CometChat.CALL_TYPE.VIDEO;
var receiverType = CometChat.RECEIVER_TYPE.USER;

var call = new CometChat.Call(receiverID, callType, receiverType);

CometChat.initiateCall(call).then(
(outGoingCall) => {
console.log("Call initiated successfully:", outGoingCall);
// perform action on success. Like show your calling screen.
},
(error) => {
console.log("Call initialization failed with exception:", error);
}
);

This method takes an object of the Call class. The constructor for Call class takes the following parameters:

ParameterDescription
receiverIDThe UID or GUID of the recipient
receiverTypeThe type of the receiver can be,

1.CometChat.RECEIVER_TYPE.USER
2.CometChat.RECEIVER_TYPE.GROUP
callTypeThe type of call can be,

1.CometChat.CALL_TYPE.AUDIO
2.CometChat.CALL_TYPE.VIDEO

On successful initialization, a Call object is returned with the details of the call including a unique session ID for the call.

Receive Calls

Wherever you wish to receive the call events in, you need to register the CallListener listener using the addCallListener() method. onCallEndedMessageReceived() event will receive when you trigger CometChat.endCall(sessionID).

var listnerID = "UNIQUE_LISTENER_ID";
CometChat.addCallListener(
listnerID,
new CometChat.CallListener({
onIncomingCallReceived: (call) => {
console.log("Incoming call:", call);
// Handle incoming call
},
onOutgoingCallAccepted: (call) => {
console.log("Outgoing call accepted:", call);
// Outgoing Call Accepted
},
onOutgoingCallRejected: (call) => {
console.log("Outgoing call rejected:", call);
// Outgoing Call Rejected
},
onIncomingCallCancelled: (call) => {
console.log("Incoming call calcelled:", call);
},
onCallEndedMessageReceived: (call) => {
console.log("CallEnded Message:", call);
},
})
);
ParametrerDescription
listenerIDAn ID that uniquely identifies that listener. We recommend using the activity or fragment name

We recommend you remove the listener once the activity or fragment is not in use.

var listenerID = "UNIQUE_LISTENER_ID";

CometChat.removeCallListener(listenerID);

As mentioned in the Overview section, Once the call is initiated, there are three options that can be possible:

  1. The receiver of the call accepts the call.
  2. The receiver of the call rejects the call.
  3. The initiator of the call cancels the call.

Please find below how these three scenarios can be implemented:

Accept the Incoming Call

Once you have received an incoming call from a user or in any group, to accept the call using the acceptCall() method.

var sessionID = "SESSION_ID";

CometChat.acceptCall(sessionID).then(
(call) => {
console.log("Call accepted successfully:", call);
// start the call using the startSession() method
},
(error) => {
console.log("Call acceptance failed with error", error);
// handle exception
}
);
ParameterDescription
sessionIDThe unique session ID available in the Call object

Reject the Incoming Call

As the call is being rejected by the receiver of the call, you need to pass the status as CometChat.CALL_STATUS.REJECTED to the rejectCall() method.

var sessionID = "SESSION_ID";
var status = CometChat.CALL_STATUS.REJECTED;

CometChat.rejectCall(sessionID, status).then(
(call) => {
console.log("Call rejected successfully", call);
},
(error) => {
console.log("Call rejection failed with error:", error);
}
);
ParameterDescription
sessionIDThe unique session ID available in the Call object.
statusReason for rejection of the call.

Cancel the Outgoing Call

As the call is being canceled by the initiator of the call, you need to pass the status as CometChat.CALL_STATUS.CANCELLED to the rejectCall() method.

var sessionID = "SESSION_ID";
var status = CometChat.CALL_STATUS.CANCELLED;

CometChat.rejectCall(sessionID, status).then(
(call) => {
console.log("Call canceled successfully", call);
},
(error) => {
console.log("Call cancelation failed with error:", error);
}
);
ParameterDescription
sessionIDThe unique session ID available in the Call object.
statusReason for rejection of the call.

Start a Call

Once the call request is sent and the receiver has accepted the call, both the initiator and the receiver need to call the startSession() method.