You can integrate Sendbird Calls to Sendbird Chat to provide users with a seamless experience in using Calls and Chat services by allowing them to make a call within a chat channel.
With the implementation of Calls integration to Chat, the call screen will appear when the call starts and when the call ends users will return to the chat view.
Note: To turn on Calls integration to Chat on the Sendbird Dashboard, go to Settings > Chat > Messages.
Since Calls integration to Chat is a way to add call capabilities to Sendbird Chat, it requires an app that uses Chat. If you already have one, you are ready to move to the next step. If you don’t have an app, learn how to set up Sendbird Chat from our Send first message page.
To use Calls integration to Chat, an environment setup is first needed for both Sendbird Calls and Sendbird Chat using the SDKs. To learn more about each, refer to Sendbird Calls for Quickstart and Sendbird Chat samples.
Sendbird Calls SDK and Chat SDK both use singleton interfaces. Since these two SDKs work independently, create appropriate functions that can handle them together.
Find your application ID from the Sendbird Dashboard to use in the Calls SDK and the Chat SDK.
// SendBirdHelper.swift
// Initialize an instance for each of SendBirdCall and SendbirdChat to use APIs in your app.
static public func configure(_ appID: String) {
SendBirdCall.configure(appId: appID)
let params = InitParams(appId: appID)
SendbirdChat.initialize(params: params)
}
To log in to the Calls and Chat SDKs, create a function that allows you to authenticate the Calls SDK and the Chat SDK together.
// SendBirdHelper.swift
// The user ID should be unique to your Sendbird application.
static public func login(userId: String, nickname: String, completionHandler: ((_ user: SendBirdUser?, _ error: Error?) -> Void)?) {
var loginError: Error?
var chatUser: SendbirdChatModule.User?
var callUser: User?
let group = DispatchGroup()
group.enter()
SendBirdCall.authenticate(with: AuthenticateParams(userId: userId)) { (user, error) in
defer { group.leave() }
guard error == nil else {
loginError = error
return
}
callUser = user
}
group.enter()
SendbirdChat.connect(userId: userId) { (user, error) in
defer { group.leave() }
guard error == nil else {
loginError = error
return
}
chatUser = user
}
// A function to authenticate the user to the Calls and Chat SDKs has been created successfully.
...
group.notify(queue: executionQueue) {
guard let user = SendBirdUser(chatUser: chatUser, callUser: callUser) else {
completionHandler?(nil, loginError)
return
}
completionHandler?(user, nil)
// The user has been successfully authenticated using this function.
}
}
As written above, the SendBirdCall.authenticate() method and SendbirdChat.connect(userId:completionHandler:) method are used to authenticate the current user to use Calls and Chat APIs in your app.
Step 4: Log out from the Calls SDK and the Chat SDK
Push notifications services allow users to receive notifications when the app is in the background. Before utilizing this feature, you need to register appropriate push tokens to Sendbird server. To turn on this feature, go to Settings > Chat > Push notifications in your dashboard.
For Calls integration to Chat, both CallKit and PushKit are used to register push tokens.
In AppDelegate.swift, save the remote device token and PushKit token from the following delegate methods:
For integration between the Calls and Chat services, the Calls SDK provides a specific option when dialing. You can provide the group channel URL to a DialParams object of Sendbird Calls as shown below:
let chatOptions = SendBirdChatOptions(channelURL: self.channel.channelUrl)
let dialParams = DialParams(calleeId: CALLEE_ID, isVideoCall: IS_VIDEO_CALL, callOptions: CallOptions(), sendbirdChatOptions: chatOptions)
SendBirdCall.dial(with: dialParams) { (call, error) in
if error == nil {
// Handle error
}
// The call has been made successfully.
...
}
When a group channel URL is provided to the DialParams object, messages containing call information such as calling statuses and duration will be automatically sent to the channel when the call starts and ends.
The messages will contain call information in the plugins field of the BaseMessage instance which can be used to properly show information about the calls.
The sample app for Calls integration to Chat is built based on Sendbird Chat. In the Chat sample app, every chat message is associated with a specific type of instance from the UITableViewCell class. With different types of messages such as user message, file message, and admin message, different types of instances of the UITableViewCell class are shown to the user, offering a more intuitive user experience.
For UI components for Calls integration to Chat, create an instance of the UITableViewCell class, like the following, that will be shown to users when they make or receive a call:
An example of UI components you can create is demonstrated in these view controller files in the sample app:
Use the UI components created to integrate Sendbird Calls to the group channel view controller in Chat. Register the UITableViewCell class to the UITableView instance of the chat view controller.
Then, add the following to the viewDidLoad() method of GroupChannelChatViewController.swift file from the Chat sample app, which will create new table view cells with the identifier CallMessageTableViewCell.
To show the registered UI components, you have to identify which messages are associated with the Calls SDK and show the messages by using the CallMessageTableViewCell class.
The BaseMessage class from Sendbird Chat SDK has a field called plugins where you can store additional information to default values. The key-value plugins are delivered as [String: Any] dictionary.
When a call is made from the Calls SDK, the plugin field of a message associated with the call will contain the following information: vendor: sendbird, type: call.
Then, for the messages that have these fields, show the UI component created.
In the tableView() method of the GroupChannelChatViewController.swift file from the Chat sample, add the following:
// GroupChannelChatViewController.swift
if currMessage is UserMessage {
guard let userMessage = currMessage as? UserMessage else { return cell }
if userMessage.plugin["vendor"] == "sendbird", userMessage.plugin["service"] == "call" {
let callTableViewCell = tableView.dequeueReusableCell(withIdentifier: "CallMessageTableViewCell") as? CallMessageTableViewCell
...
}
}
The call view controller provides interfaces for actions such as ending a call, muting or unmuting , or offers local and remote video views on a user's screen.
To implement a voice call interface to your app, refer to VoiceCallViewController.swift and to implement a video call interface, refer to VideoCallViewController.swift on our Sendbird Calls for Quickstart. Once both interfaces are implemented, the two will be referred to as CallingViewController.
Calls integration to Chat can provide a seamless user experience by allowing users to initiate a new call directly from a channel by tapping the messages that contain call information.
First, add an UITapGestureRecognizer instance to the CallMessageCell instance:
If you would like to make a call again, tap any one of the status messages and a pop-up will appear for voice or video call options.
Create an UIAlertController instance and add audioCallAction and videoCallAction as action items for initiating a new voice or video call. The view controller then will appear for the new call.
To enhance the user experience for Calls integration to Chat, features such as receiving calls using CallKit and push notifications should be added. Refer to Calls integration to Chat for Quickstart and learn more about these essential features.