iOS v4 SDK: How to setup Voice and Video calling with iOS SDK

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:

  1. Set up the Chat SDK:
    The Chat SDK is necessary to manage events like call initiation, acceptance, and rejection.
  1. Calling Workflow:
    Let’s consider Alex as the caller and Bob as the receiver.
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 the CallListener. He can:
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

  1. 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:
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))")
}
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 the getAuthToken() 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.

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:

  1. 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))")
}
  1. Generate a Call Token:
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 the getAuthToken() method. Alternatively, you can generate it using the REST API.
  1. 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.