CometChat offers developers two options for integrating voice and video calls into websites and mobile apps: Default Calling and Direct Calling. These provide distinct user experiences, similar to WhatsApp and Google Meet/Zoom respectively.
1. Default Calling
The Default Calling option allows for one-on-one calls, where users can accept or reject an incoming call, just like WhatsApp. This method requires the integration of both the CometChat Chat SDK and Calls SDK.
Steps for Integration:
- Set up the Chat SDK:
The Chat SDK is necessary to manage events like call initiation, acceptance, and rejection.
-
Logging into the SDKAfter these steps, you can perform CometChat operations using the SDK.
- Calling Workflow:
Let’s consider Alex as the caller and Bob as the receiver.
- Alex initiates the call using the initiateCall() method.
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);
}
- Bob receives the call in the
onIncomingCallReceived()
method of theCallListener
. He can:- Accept the call using the acceptCall() method, or
CometChat.acceptCall(sessionID: incomingCall!.sessionID, onSuccess: { (ongoing_call) in
print("Accepted Call. " + call!.stringValue());
}) { (error) in
print("Call accepting failed with error: " + error!.errorDescription);
}
- Reject the call using the rejectCall() method with a
rejected
status.
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);
}
- Alex can cancel the call before it’s accepted using the rejectCall() method with a
cancelled
status.
let status: CometChatConstants.callStatus = .cancelled;
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);
}
- If Bob accepts the call, the flow will move to the Calls SDK. For more information, please refer to point (3) below
For more detailed methods, visit: Default Calling Docs
- Calls SDK Setup:
Once the call is accepted, the interaction moves to the Calls SDK for the actual voice/video session. Steps to get started with the Calls SDK:
- Initialise the Calls SDK:
- Follow the setup guide: Initialise the Calls SDK
guard let callAppSettings = callAppSettings else { return }
CometChatCalls.init(callsAppSettings: callAppSettings) { success in
print("CometChatCalls init success: \\(success)")
} onError: { error in
print("CometChatCalls init error: \\(String(describing: error?.errorDescription))")
}
- Generate a Call Token:
- To generate a token for the user, use the Generate Call Token method.
var callToken: String?
CometChatCalls.generateToken(authToken: authToken as NSString, sessionID: sessionId) { token in
self.callToken = token
} onError: { error in
print("CometChatCalls generateToken error: \(String(describing: error?.errorDescription))")
}
- If you’re using the Chat SDK, you can obtain an
authToken
for the user via thegetAuthToken()
method. Alternatively, you can generate it using the REST API. - Start the Call Session:
- Once the token is generated, call the
startSession()
method to begin the call. - In case of Default Call, the session id will be generated by CometChat and provided to you while initiating the call and via the events available in the CallListener.
- Once the token is generated, call the
2. Direct Calling
The Direct Calling option supports multi-user sessions with a shared session ID, similar to Google Meet or Zoom. This type of calling only requires the Calls SDK.
Steps for Integration:
- Initialise the Calls SDK:
- Follow the setup guide: Initialise the Calls SDK
guard let callAppSettings = callAppSettings else { return }
CometChatCalls.init(callsAppSettings: callAppSettings) { success in
print("CometChatCalls init success: \\(success)")
} onError: { error in
print("CometChatCalls init error: \\(String(describing: error?.errorDescription))")
}
- Generate a Call Token:
- To generate a token for the user, use the Generate Call Token method.
var callToken: String?
CometChatCalls.generateToken(authToken: authToken as NSString, sessionID: sessionId) { token in
self.callToken = token
} onError: { error in
print("CometChatCalls generateToken error: \(String(describing: error?.errorDescription))")
}
- If you’re using the Chat SDK, you can obtain an
authToken
for the user via thegetAuthToken()
method. Alternatively, you can generate it using the REST API.
- Start the Call Session:
- Once the token is generated, call the
startSession()
method to begin the call.
guard let callToken = self.callToken, let callSettings = self.callSettings, let view = callView else { return }
CometChatCalls.startSession(callToken: callToken, callSetting: callSettings, view: view) { success in
print("CometChatCalls startSession success: \(success)")
} onError: { error in
print("CometChatCalls startSession error: \(String(describing: error?.errorDescription))")
}
- You will need to generate a unique session ID for this type of call, ensuring that all users connecting with the same ID join the same session.
With CometChat’s flexible options for Default Calling and Direct Calling, you can easily integrate one-on-one or group call functionalities, tailored to your application’s needs.
For more information, refer to the official CometChat SDK documentation.