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.
- Setting up and Initializing the SDK
- Logging into the SDK After 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.
private String receiverID = "UID";
private String receiverType = CometChatConstants.RECEIVER_TYPE_USER;
private String callType = CometChatConstants.CALL_TYPE_VIDEO;
Call call = new Call(receiverID, receiverType, callType);
CometChat.initiateCall(call, new CometChat.CallbackListener<Call>() {
@Override
public void onSuccess(Call call) {
Log.d(TAG, "Call initiated successfully: " + call.toString());
}
@Override
public void onError(CometChatException e) {
Log.d(TAG, "Call initialization failed with exception: " + e.getMessage());
}
});
- Bob receives the call in the
onIncomingCallReceived()
method of theCallListener
. He can:- Accept the call using the acceptCall() method, or
private String sessionID = "SESSION_ID";
CometChat.acceptCall(sessionID, new CometChat.CallbackListener<Call>() {
@Override
public void onSuccess(Call call) {
}
public void onError(CometChatException e) {
}
});
- Reject the call using the rejectCall() method with a
rejected
status.
private String sessionID = "SESSION_ID";
private String status = CometChatConstants.CALL_STATUS_REJECTED;
CometChat.rejectCall(sessionID, status, new CometChat.CallbackListener<Call>() {
@Override
public void onSuccess(Call call) {
}
@Override
public void onError(CometChatException e) {
}
});
- Alex can cancel the call before it’s accepted using the rejectCall() method with a
cancelled
status.
private String sessionID = "SESSION_ID";
private String status = CometChatConstants.CALL_STATUS_CANCELLED;
CometChat.rejectCall(sessionID, status, new CometChat.CallbackListener<Call>() {
@Override
public void onSuccess(Call call) {
}
@Override
public void onError(CometChatException e) {
}
});
- 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
private Context context = this;
private String appID = "APP_ID"; // Replace with your App ID
private String region = "REGION"; // Replace with your App Region ("eu" or "us")
CallAppSettings callAppSettings = CallAppSettingBuilder()
.setAppId(appID)
.setRegion(region)
.build();
CometChatCalls.init(
context,
callAppSettings,
new CometChatCalls.CallbackListener<String>() {
override public void onSuccess(String successMessage) {
}
override public void onError(CometChatException e) {
}
}
)
- Generate a Call Token:
- To generate a token for the user, use the Generate Call Token method.
String sessionId = "" //Random or available in call obecjt in case of default calling
String userAuthToken = CometChat.getUserAuthToken() //Logged in user auth token
CometChatCalls.generateToken(sessionId, userAuthToken, new CometChatCalls.CallbackListener<GenerateToken>() {
@Override
public void onSuccess(GenerateToken generateToken) {
}
@Override
public void onError(com.cometchat.pro.rtc.exceptions.CometChatException e) {
}
});
- 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
private Context context = this;
private String appID = "APP_ID"; // Replace with your App ID
private String region = "REGION"; // Replace with your App Region ("eu" or "us")
CallAppSettings callAppSettings = CallAppSettingBuilder()
.setAppId(appID)
.setRegion(region)
.build();
CometChatCalls.init(
context,
callAppSettings,
new CometChatCalls.CallbackListener<String>() {
override public void onSuccess(String successMessage) {
}
override public void onError(CometChatException e) {
}
}
)
- Generate a Call Token:
- To generate a token for the user, use the Generate Call Token method.
String sessionId = "" //Random or available in call obecjt in case of default calling
String userAuthToken = CometChat.getUserAuthToken() //Logged in user auth token
CometChatCalls.generateToken(sessionId, userAuthToken, new CometChatCalls.CallbackListener<GenerateToken>() {
@Override
public void onSuccess(GenerateToken generateToken) {
}
@Override
public void onError(com.cometchat.pro.rtc.exceptions.CometChatException e) {
}
});
- 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.
RelativeLayout videoContainer;
Context activityContext = this; //Your activity reference
String callToken = "" //Received on generate token onSuccess
boolean isDefaultCall = true; //Call type default or direct.
boolean isCallEndedByMe = false;
CallSettings callSettings = new CometChatCalls.CallSettingsBuilder(activityContext, videoContainer)
.setDefaultLayoutEnable(true)
.setIsAudioOnly(false)
.setEventListener(new CometChatCallsEventsListener() {
@Override
public void onCallEnded() {
if(isDefaultCall){
if(isCallEndedByMe){
//Call CometChat.endCall() method
}else {
//Call CometChatCalls.endSession() and CometChat.clearActiveCall() methods
}
} else{
//If a group call has ended and there is only one user left on the call
//you can end the call session for that user by calling the CometChatCalls.endSession()
}
}
@Override
public void onCallEndButtonPressed() {
if(isDefaultCall){
isCallEndedByMe = true;
}else {
//Leave the current call
CometChatCalls.endSession();
}
}
@Override
public void onUserJoined(RTCUser user) {
}
@Override
public void onUserLeft(RTCUser user) {
}
@Override
public void onUserListChanged(ArrayList<RTCUser> users) {
}
@Override
public void onAudioModeChanged(ArrayList<AudioMode> devices) {
}
@Override
public void onCallSwitchedToVideo(CallSwitchRequestInfo callSwitchRequestInfo) {
}
@Override
public void onUserMuted(RTCMutedUser muteObj) {
}
@Override
public void onRecordingToggled(RTCRecordingInfo recordingInfo) {
}
@Override
public void onError(com.cometchat.pro.rtc.exceptions.CometChatException ce) {
}
})
.build();
CometChatCalls.startSession(callToken, callSettings, new CometChatCalls.CallbackListener<String>() {
@Override
public void onSuccess(String s) {
}
@Override
public void onError(com.cometchat.pro.rtc.exceptions.CometChatException e) {
}
});
- 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.