Default Calling
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:
a. Accept the call from Alex using theacceptCall()
method.
b. Reject the call from Alex using therejectCall("rejected")
method passing the status asrejected
. -
In the meantime, Alex has the option to cancel the call he initiated to Bob using the
rejectCall("cancelled")
method passing the status ascancelled
. -
If Bob accepts the call from Alex, both Alex and Bob need to call the
startCall()
method. Alex in theonIncomingCallReceived()
method of theCallListener
and Bob in the success obtained from the call toacceptCall()
method and both will be connected to each other.
Initiate Call
The initiateCall()
method sends a call request to a user or a group.
- Swift
- Objective C
let receiverID = "UID"
let receiverType:CometChat.ReceiverType = .user OR.group
let callType: CometChat.CallType = .audio OR .video
let newCall = Call(receiverId: receiverID, callType: callType, receiverType: receiverType);
CometChat.initiateCall(call: newCall, onSuccess: { (ongoing_call) in
print("Call initiated successfully " + ongoing_call!.stringValue());
}) { (error) in
print("Call initialization failed with error: " + error!.errorDescription);
}
NSString *receiverID = @"UID";
Call *newCall = [[Call alloc]initWithReceiverId:receiverID callType:CallTypeVideo receiverType:ReceiverTypeUser];
[CometChat initiateCallWithCall:newCall onSuccess:^(Call * ongoing_call) {
NSLog(@"Call initiated successfully. %@",[ongoing_call stringValue]);
} onError:^(CometChatException * error) {
NSLog(@"Call initialization failed with error: %@",[error ErrorDescription]);
}];
This method takes an object of the Call
class. The constructor for Call
class takes the following parameters:
Properties | |
---|---|
Parameter | Descriptions |
receiverID | The UID or GUID of the recipient |
receiverType | The type of the receiver viz. - user - group |
callType | The type of the receiver viz. - audio - video |
Receive Calls
In order to receive all call
events, you must add protocol conformance CometChatCallDelegate
as Shown Below :
- Swift
- Objective C
extension ViewController: CometChatCallDelegate {
func onIncomingCallReceived(incomingCall: Call ? , error : CometChatException ? ) {
print(" Incoming call " + incomingCall!.stringValue());
}
func onOutgoingCallAccepted(acceptedCall: Call ? , error : CometChatException ? ) {
print("Outgoing call " + acceptedCall!.stringValue());
}
func onOutgoingCallRejected(rejectedCall: Call ? , error : CometChatException ? ) {
print("Rejected call " + rejectedCall!.stringValue());
}
func onIncomingCallCanceled(canceledCall: Call ? , error : CometChatException ? ) {
print("Cancelled call " + canceledCall!.stringValue());
}
func onCallEndedMessageReceived(endedCall: Call?, error: CometChatException?) {
print(" Ended call " + endedCall!.stringValue())
}
}
@interface ViewController ()<CometChatCallDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[CometChat setCalldelegate:self];
}
- (void)onIncomingCallCanceledWithCanceledCall:(Call * _Nullable)canceledCall error:(CometChatException * _Nullable)error {
NSLog(@"Incoming call cancelled %@",[canceledCall stringValue]);
}
- (void)onIncomingCallReceivedWithIncomingCall:(Call * _Nullable)incomingCall error:(CometChatException * _Nullable)error {
NSLog(@"Incoming call %@",[incomingCall stringValue]);
}
- (void)onOutgoingCallAcceptedWithAcceptedCall:(Call * _Nullable)acceptedCall error:(CometChatException * _Nullable)error {
NSLog(@"Outgoing call accepted %@",[acceptedCall stringValue]);
}
- (void)onOutgoingCallRejectedWithRejectedCall:(Call * _Nullable)rejectedCall error:(CometChatException * _Nullable)error {
NSLog(@"Outgoing call rejected %@",[rejectedCall stringValue]);
}
@end
Do not forget to set your view controller as a CometChat delegate probably in viewDidLoad()
as CometChat.calldelegate = self
As mentioned in the Integration 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
in onIncomingCallReceived(incomingCall:, error:)
, you need to accept the call using the acceptCall()
method.
- Swift
- Objective C
CometChat.acceptCall(sessionID: incomingCall!.sessionID, onSuccess: { (ongoing_call) in
print("Accepted Call. " + call!.stringValue());
}) { (error) in
print("Call accepting failed with error: " + error!.errorDescription);
}
[CometChat acceptCallWithSessionID:incomingCall.sessionID onSuccess:^(Call * ongoing_call) {
NSLog(@"Accepted call. %@",[ongoing_call stringValue]);
} onError:^(CometChatException * error) {
NSLog(@"Call accepting failed with error: %@",[error ErrorDescription]);
}];
Reject the incoming Call
You can also choose to reject the incoming call once it is received using the rejectCall()
method.
- Swift
- Objective C
let status: CometChatConstants.callStatus = .rejected;
CometChat.rejectCall(sessionID: (incomingCall?.sessionID)!, status: status, onSuccess: { (rejeceted_call) in
print("Call rejected successfully. " + rejeceted_call!.stringValue());
}) { (error) in
print("Call rejection failed with error: " + error!.errorDescription);
}
[CometChat rejectCallWithSessionID:incomingCall.sessionID status:callStatusRejected onSuccess:^(Call * rejeceted_call) {
__ Success
NSLog("Call rejected successfully %@ " , [rejeceted_call stringValue]);
} onError:^(CometChatException * error) {
__ Error
NSLog("Call rejection failed with exception: %@" + [error ErrorDescription]);
}];
Properties | |
---|---|
status | Reason for rejection of the call |
sessionID | The unique session ID available in the Call object |
Here the status needs to be set as CometChatConstants.callStatus.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.callStatus.cancelled
The possible values for the status can be one of the below:
Description | ||
---|---|---|
status | interpretation | Enum available |
rejected | The receiver rejects the call as it is received without accepting the call | .rejected |
cancelled | The initiator ends the call without the receiver accepting the call | .cancelled |
busy | The receiver rejects the call, as he/she is busy on another call | .busy |
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.