Skip to main content

Overview

This section demonstrates how to implement calling functionality using only the CometChat Calls SDK, without requiring the Chat SDK. This is ideal for applications that need video/audio calling capabilities without the full chat infrastructure.
Before you begin, ensure you have completed the Calls SDK setup.

User Authentication

To start a call session, you need a user auth token. Since this implementation doesn’t use the Chat SDK, you’ll need to obtain the auth token via the CometChat REST API.
To understand user authentication in CometChat, see the User Auth documentation.
You can obtain the auth token using one of these REST API endpoints:
For testing or POC purposes, you can create an auth token directly from the CometChat Dashboard. Navigate to Users & Groups → Users, select a user, and click + Create Auth Token.
Store the auth token securely in your application for use when generating call tokens.

Generate Call Token

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. Use the generateToken() method to create a call token:
var callToken: GenerateToken?

let sessionId = "UNIQUE_SESSION_ID" // Generate a unique session ID
let userAuthToken = "USER_AUTH_TOKEN" // Obtained from REST API

CometChatCalls.generateToken(authToken: userAuthToken as NSString, sessionID: sessionId) { token in
    self.callToken = token
} onError: { error in
    print("Token generation failed: \(String(describing: error?.errorDescription))")
}
ParameterDescription
sessionIdA unique session ID for the call. Generate this yourself or use a shared ID for participants to join the same call.
userAuthTokenThe user auth token obtained from the CometChat REST API.

Start Call Session

Use the startSession() method to join a call session. This method requires a call token and a CallSettings object.
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))")
}

Call Settings

Configure the call experience using the following CallSettingsBuilder methods:
MethodDescription
setDefaultLayout(Bool)Enables or disables the default call UI layout with built-in controls. true shows the default layout. Default: true
setIsAudioOnly(Bool)Sets whether the call is audio-only or audio-video. true for audio-only. Default: false
setIsSingleMode(Bool)Enables single participant mode.
setShowSwitchToVideoCall(Bool)Shows or hides the switch to video call button.
setEnableVideoTileClick(Bool)Enables or disables click interactions on video tiles. Default: true
setEnableDraggableVideoTile(Bool)Enables or disables drag functionality for video tiles in Spotlight mode. Default: true
setEndCallButtonDisable(Bool)Shows or hides the end call button. Default: false (shown)
setShowRecordingButton(Bool)Shows or hides the recording button. Default: false
setSwitchCameraButtonDisable(Bool)Shows or hides the switch camera button. Default: false (shown)
setMuteAudioButtonDisable(Bool)Shows or hides the mute audio button. Default: false (shown)
setPauseVideoButtonDisable(Bool)Shows or hides the pause video button. Default: false (shown)
setAudioModeButtonDisable(Bool)Shows or hides the audio mode selection button. Default: false (shown)
setStartAudioMuted(Bool)Starts the call with the microphone muted. Default: false
setStartVideoMuted(Bool)Starts the call with the camera turned off. Default: false
setMode(DisplayModes)Sets the call UI layout mode. Available: .default, .single, .spotlight. Default: .default
setAvatarMode(AvatarMode)Sets avatar display mode. Available: .circle, .square, .fullscreen. Default: .circle
setDefaultAudioMode(AudioMode)Sets the initial audio output device. Available: SPEAKER, EARPIECE, BLUETOOTH, HEADPHONES
setIdleTimeoutPeriod(Int)Sets idle timeout in seconds. Warning appears 60 seconds before auto-termination. Default: 180. v4.1.1+
setDelegate(self)Sets the delegate to receive call events.

Call Listeners

The CallsEventsDelegate protocol provides real-time callbacks for call session events.
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
        CometChatCalls.endSession()
        // Close the calling screen
    }
    
    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
    }
}

Events

EventDescription
onCallEnded()Invoked when the call session terminates for a 1:1 call. Both participants receive this callback.
onSessionTimeout()Invoked when the call is auto-terminated due to inactivity (default: 180 seconds). v4.1.1+
onCallEndButtonPressed()Invoked when the local user taps the end call button. Call CometChatCalls.endSession() to leave the session.
onUserJoined(rtcUser: RTCUser)Invoked when a remote participant joins.
onUserLeft(rtcUser: RTCUser)Invoked when a remote participant leaves.
onUserListChanged(rtcUsers: [RTCUser])Invoked whenever the participant list changes.
onAudioModeChanged(mode: [AudioMode])Invoked when available audio devices change.
onCallSwitchedToVideo(callSwitchedInfo: CallSwitchRequestInfo)Invoked when an audio call is upgraded to video.
onUserMuted(rtcMutedUser: RTCMutedUser)Invoked when a participant’s mute state changes.
onRecordingToggled(recordingInfo: RTCRecordingInfo)Invoked when call recording starts or stops.

End Call Session

To end the call session and release all media resources, call CometChatCalls.endSession() in the onCallEndButtonPressed() callback.
func onCallEndButtonPressed() {
    CometChatCalls.endSession()
    // Close the calling screen
}

Methods

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.

Switch Camera

Toggles between the front and rear camera during a video call.
CometChatCalls.switchCamera()

Mute Audio

Controls the local audio stream transmission.
// Mute audio
CometChatCalls.audioMuted(true)

// Unmute audio
CometChatCalls.audioMuted(false)

Pause Video

Controls the local video stream transmission.
// Pause video
CometChatCalls.videoPaused(true)

// Resume video
CometChatCalls.videoPaused(false)

Set Audio Mode

Routes the audio output to a specific device.
CometChatCalls.setAudioMode(AudioMode(mode: "SPEAKER"))

Enter PIP Mode

Enters Picture-in-Picture mode.
CometChatCalls.enterPIPMode()

Exit PIP Mode

Exits Picture-in-Picture mode.
CometChatCalls.exitPIPMode()

Switch To Video Call

Upgrades an ongoing audio call to a video call.
CometChatCalls.switchToVideoCall()

Start Recording

Starts recording the call session.
CometChatCalls.startRecording()

Stop Recording

Stops an ongoing call recording.
CometChatCalls.stopRecording()

End Call

Terminates the current call session and releases all media resources.
CometChatCalls.endSession()