Default Call
This section will provide information on how a complete calling workflow can be set up using CometChat. 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.
-
Alex initiates the call to Bob using the initiateCall() method.
-
Bob now has two choices:
- Accept the call from Alex using the acceptCall() method.
- Reject the call from Alex using the rejectCall("rejected") method passing the status as
rejected
.
-
In the meantime, Alex has the option to cancel the call he initiated to Bob using the rejectCall("cancelled") method passing the status as
cancelled
. -
If Bob accepts the call from Alex, both Alex and Bob need to call the startCall() method. Alex in the onOutgoingCallAccepted() method of the
CallListener
and Bob in the success obtained from theacceptCall()
method and both will be connected to each other.
Initiate Call
The initiateCall()
method sends a call request to a user or a group.
- Dart (User)
- Dart (Group)
String receiverID = "UID";
String receiverType = CometChatConstants.RECEIVER_TYPE_USER;
String callType = CometChatConstants.CALL_TYPE_VIDEO;
Call call = Call(receiverUid: receiverID, receiverType: receiverType, type: callType);
CometChat.initiateCall(
call,
onSuccess: (Call call){
debugPrint("Initiated");
}, onError: (CometChatException e){
debugPrint("Error: $e");
}
);
String receiverID = "UID";
String receiverType = CometChatConstants.RECEIVER_TYPE_GROUP;
String callType = CometChatConstants.CALL_TYPE_VIDEO;
Call call = Call(receiverUid: receiverID, receiverType: receiverType, type: callType);
CometChat.initiateCall(
call,
onSuccess: (Call call){
debugPrint("Initiated");
}, onError: (CometChatException e){
debugPrint("Error: $e");
}
);
This method takes an object of the Call
class. The constructor for Call
class takes the following parameters:
Parameter | Description |
---|---|
receiverID | The UID or GUID of the recipient |
receiverType | The type of the receiver viz. 1. CometChatConstants.RECEIVER_TYPE_USER 2. CometChatConstants.RECEIVER_TYPE_GROUP |
callType | The type of the call viz. 1. CometChatConstants.CALL_TYPE_AUDIO 2. CometChatConstants.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
For every activity you wish to receive the call events in, you need to register the CallListener
listener using the addCallListener()
method.
- Dart
import 'package:cometchat/cometchat_sdk.dart';
class YourClassName extends State<YourClassName> with CallListener {
void initState() {
CometChat.addCallListener(listenerId, this); //Add listener
}
void dispose() {
super.dispose();
CometChat.removeCallListener(listenerId); //Remove listener
}
void onIncomingCallCancelled(Call call) {
super.onIncomingCallCancelled(call);
debugPrint("onIncomingCallCancelled");
}
void onIncomingCallReceived(Call call) {
super.onIncomingCallReceived(call);
debugPrint("onIncomingCallReceived");
}
void onOutgoingCallAccepted(Call call) {
super.onOutgoingCallAccepted(call);
debugPrint("onOutgoingCallAccepted");
}
void onOutgoingCallRejected(Call call) {
super.onOutgoingCallRejected(call);
debugPrint("onOutgoingCallRejected");
}
void onCallEndedMessageReceived(Call call) {
super.onCallEndedMessageReceived(call);
debugPrint("onCallEndedMessageReceived");
}
}
Parametrer | Description |
---|---|
listenerID | An 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.
- Dart
String 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:
- The receiver of the call accepts the call.
- The receiver of the call rejects the call.
- 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.
- Dart
String sessionID = "SESSION_ID";
CometChat.acceptCall(
sessionId!,
onSuccess: (Call call){
debugPrint("Accepted");
}, onError: (CometChatException e){
debugPrint("Error: ${e.message}");
}
);
Parameter | Description |
---|---|
sessionID | The unique session ID available in the Call object |
Reject the Incoming Call
To reject the incoming call once it is received using the rejectCall()
method.
- Dart
String sessionID = "SESSION_ID";
String status = CometChatConstants.CALL_STATUS_REJECTED;
CometChat.rejectCall(
sessionId!,
status,
onSuccess: (Call call){
debugPrint("Rejected");
}, onError: (CometChatException e){
debugPrint("Error: ${e.message}");
}
);
Parameter | Description |
---|---|
sessionID | The unique session ID available in the Call object |
status | Reason for rejection of the call |
Here the status needs to be set as CometChatConstants.CALL_STATUS_REJECTED
as the call is being rejected by the receiver of the call.
Cancel the Outgoing Call
In the case where the initiator wishes to cancel the call, use the same above rejectCall()
method and just pass the status to the rejectCall()
method as CometChatConstants.CALL_STATUS_CANCELLED
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.