This section demonstrates how to start a call session in an iOS application. Previously known as Direct Calling.Before you begin, we strongly recommend you read the calling setup guide.
If you want to implement a complete calling experience with ringing functionality (incoming/outgoing call UI), follow the Ringing guide first. Once the call is accepted, return here to start the call session.
A call token is required for secure access to a call session. Each token is unique to a specific session and user combination, ensuring that only authorized users can join the call.You can generate the token just before starting the call, or generate and store it ahead of time based on your use case.Use the generateToken() method to create a call token:
Swift
Report incorrect code
Copy
Ask AI
var callToken: GenerateToken?let sessionId = "UNIQUE_SESSION_ID" // Random or from Call object in ringing flowlet authToken = CometChat.getUserAuthToken() // Logged in user auth tokenCometChatCalls.generateToken(authToken: authToken as NSString, sessionID: sessionId) { token in self.callToken = token} onError: { error in print("Token generation failed: \(String(describing: error?.errorDescription))")}
Parameter
Description
sessionId
The unique session ID. In ringing flow, this is available in the Call object. For standalone calls, generate a random unique string.
authToken
The logged-in user’s auth token from CometChat.getUserAuthToken().
Use the startSession() method to join a call session. This method requires a call token (generated in the previous step) and a CallSettings object that configures the call UI and behavior.The CallSettings class configures the call UI and behavior. Use callSettingsBuilder to create a CallSettings instance with the following required parameters:
Parameter
Description
view
A UIView where the calling UI will be rendered
callToken
The GenerateToken object received from generateToken() onSuccess
Swift
Report incorrect code
Copy
Ask AI
guard let callToken = self.callToken else { return }let callSettings = CometChatCalls.callSettingsBuilder .setDefaultLayout(true) .setIsAudioOnly(false) .setDelegate(self) .build()CometChatCalls.startSession(callToken: callToken, callSetting: callSettings, view: callView) { success in print("Call session started successfully")} onError: { error in print("Start session failed: \(String(describing: error?.errorDescription))")}
The CallsEventsDelegate protocol provides real-time callbacks for call session events, including participant changes, call state updates, and error conditions.To receive call events, conform to CallsEventsDelegate and set the delegate in callSettingsBuilder using setDelegate(self).
Swift
Report incorrect code
Copy
Ask AI
extension ViewController: CallsEventsDelegate { func onCallEnded() { // Call ended, close the calling screen } func onSessionTimeout() { // Session timed out due to inactivity } func onCallEndButtonPressed() { // User pressed end call button } func onUserJoined(rtcUser: RTCUser) { // A participant joined the call } func onUserLeft(rtcUser: RTCUser) { // A participant left the call } func onUserListChanged(rtcUsers: [RTCUser]) { // Participant list updated } func onAudioModeChanged(mode: [AudioMode]) { // Available audio devices changed } func onCallSwitchedToVideo(callSwitchedInfo: CallSwitchRequestInfo) { // Call upgraded from audio to video } func onUserMuted(rtcMutedUser: RTCMutedUser) { // A participant's mute state changed } func onRecordingToggled(recordingInfo: RTCRecordingInfo) { // Recording started or stopped }}
Method:onCallEnded()Triggered: When call session terminates (1:1 call)Payload: NoneAction: Clean up and close calling screen
Method:onSessionTimeout()Triggered: When call auto-terminates due to inactivityPayload: NoneDefault timeout: 180 seconds (configurable via setIdleTimeoutPeriod)
Method:onCallEndButtonPressed()Triggered: When local user taps end call buttonPayload: NoneAction: End call (different for Ringing vs Session Only flow)
Method:onUserJoined(rtcUser: RTCUser)Triggered: When a remote participant joins the callRTCUser Object:
Property
Type
Description
uid
String?
User’s unique identifier
name
String?
User’s display name
avatar
String?
User’s avatar URL
Method:onUserLeft(rtcUser: RTCUser)Triggered: When a remote participant leaves the callRTCUser Object:
Property
Type
Description
uid
String?
User’s unique identifier
name
String?
User’s display name
avatar
String?
User’s avatar URL
Method:onUserListChanged(rtcUsers: [RTCUser])Triggered: Whenever the participant list changesPayload: Array of all current participantsRTCUser Object:
Property
Type
Description
uid
String?
User’s unique identifier
name
String?
User’s display name
avatar
String?
User’s avatar URL
Method:onAudioModeChanged(mode: [AudioMode])Triggered: When available audio devices changePayload: Array of available AudioMode optionsAudioMode Values:
Mode
Description
SPEAKER
Phone speaker
EARPIECE
Phone earpiece
BLUETOOTH
Connected Bluetooth device
HEADPHONES
Wired headphones
Method:onCallSwitchedToVideo(callSwitchedInfo: CallSwitchRequestInfo)Triggered: When an audio call is upgraded to videoCallSwitchRequestInfo Object:
Property
Type
Description
requestedBy
RTCUser?
User who requested the switch
Method:onUserMuted(rtcMutedUser: RTCMutedUser)Triggered: When a participant’s mute state changesRTCMutedUser Object:
Property
Type
Description
uid
String?
User’s unique identifier
name
String?
User’s display name
audioMuted
Bool?
Is audio muted
videoMuted
Bool?
Is video muted
Method:onRecordingToggled(recordingInfo: RTCRecordingInfo)Triggered: When call recording starts or stopsRTCRecordingInfo Object:
Ending a call session properly is essential to release media resources (camera, microphone, network connections) and update call state across all participants.
Sample Payloads - End Call
Ringing Flow
Session Only Flow
endCall Response
When using the Ringing flow, coordinate between Chat SDK and Calls SDK.User who initiates end call (presses end button):
When using the Ringing flow, you must coordinate between the CometChat Chat SDK and the Calls SDK to properly terminate the call.
The Ringing flow requires calling methods from both the Chat SDK (CometChat.endCall()) and the Calls SDK (CometChatCalls.endSession()) to ensure proper call termination.
User who initiates the end call:When the user presses the end call button, the onCallEndButtonPressed() callback is triggered. Inside this callback, call CometChat.endCall(). On success, call CometChat.clearActiveCall() and CometChatCalls.endSession().
Swift
Report incorrect code
Copy
Ask AI
func onCallEndButtonPressed() { CometChat.endCall(sessionID: sessionId) { call in CometChat.clearActiveCall() CometChatCalls.endSession() // Close the calling screen } onError: { error in print("End call failed: \(String(describing: error?.errorDescription))") }}
These methods are available for performing custom actions during an active call session. Use them to build custom UI controls or implement specific behaviors based on your use case.
These methods can only be called when a call session is active.