Introduction
Integrate Chat using PubSub in your iOS Video Call App to enhance user communication and experience. With PubSub, real-time messaging becomes seamless, allowing users to exchange messages effortlessly during video calls. This integration adds a layer of interactivity, enabling users to chat, share information, and collaborate seamlessly while engaging in video conversations. By incorporating PubSub technology, your app ensures reliable message delivery and synchronization across all connected devices, enhancing the overall user experience.
Benefits of Integrate Chat using PubSub in iOS Video Call App:
- Real-time Communication: PubSub integration enables real-time messaging within your iOS Video Call App, enhancing user interaction during video calls.
- Seamless Collaboration: Users can share information, exchange messages, and collaborate effectively while engaged in video conversations, fostering teamwork and productivity.
- Enhanced User Experience: The addition of PubSub technology enriches the app's functionality, providing a smoother and more interactive experience for users.
- Reliable Message Delivery: PubSub ensures reliable message delivery and synchronization across all connected devices, eliminating delays and ensuring messages are delivered promptly.
- Scalability: PubSub architecture allows your app to scale effortlessly, accommodating a growing user base without compromising performance.
Use Case of Integrate Chat using PubSub in iOS Video Call App:
- Real-time Collaboration: While discussing project details via video call, team members can use the integrated chat feature powered by PubSub to share documents, links, and updates instantly.
- Instant Feedback: Team members can provide instant feedback or ask questions via chat without interrupting the flow of the video call, enhancing communication efficiency.
- Document Sharing: With PubSub, users can seamlessly share project documents, screenshots, or any necessary files directly within the chat, facilitating collaboration and decision-making.
In this guide, we'll walk you through the process of seamlessly adding chat features to your iOS video-calling application. From setting up the chat environment to handling chat interactions within your video call interfaces, we'll cover all the essential steps to improve your app's functionality and user experience.
Getting Started with VideoSDK
To integrate the Chat Feature, we must use the capabilities that VideoSDK offers. Before diving into the implementation steps, let's ensure you complete the necessary prerequisites.
Create a VideoSDK Account
Go to your VideoSDK dashboard and sign up if you don't have an account. This account gives you access to the required Video SDK token, which acts as an authentication key that allows your application to interact with VideoSDK functionality.
Generate your Auth Token
Visit your VideoSDK dashboard and navigate to the "API Key" section to generate your auth token. This token is crucial in authorizing your application to use VideoSDK features. For a more visual understanding of the account creation and token generation process, consider referring to the provided tutorial.
Prerequisites and Setup
- iOS 11.0+
- Xcode 12.0+
- Swift 5.0+
This App will contain two screens:
Join Screen: This screen allows the user to either create a meeting or join the predefined meeting.
Meeting Screen: This screen basically contains local and remote participant views and some meeting controls such as Enable/Disable the microphone or Camera and Leave the meeting.
Integrate VideoSDK
To install VideoSDK, you must initialize the pod on the project by running the following command:
pod init
It will create the pod file in your project folder, Open that file and add the dependency for the VideoSDK, like below:
pod 'VideoSDKRTC', :git => 'https://github.com/videosdk-live/videosdk-rtc-ios-sdk.git'
then run the below code to install the pod:
pod install
then declare the permissions in Info.plist :
<key>NSCameraUsageDescription</key>
<string>Camera permission description</string>
<key>NSMicrophoneUsageDescription</key>
<string>Microphone permission description</string>
Project Structure
iOSQuickStartDemo
├── Models
├── RoomStruct.swift
└── MeetingData.swift
├── ViewControllers
├── StartMeetingViewController.swift
└── MeetingViewController.swift
├── AppDelegate.swift // Default
├── SceneDelegate.swift // Default
└── APIService
└── APIService.swift
├── Main.storyboard // Default
├── LaunchScreen.storyboard // Default
└── Info.plist // Default
Pods
└── Podfile
Create models
Create a swift file for MeetingData
and RoomStruct
class model for setting data in object pattern.
Essential Steps for Building the Video Calling
This guide is designed to walk you through the process of integrating Chat with VideoSDK. We'll cover everything from setting up the SDK to incorporating the visual cues into your app's interface, ensuring a smooth and efficient implementation process.
Step 1: Get started with APIClient
Before jumping to anything else, we have to write an API to generate a unique meetingId
. You will require an authentication token; you can generate it either using videosdk-server-api-example or from the VideoSDK Dashboard for developers.
Step 2: Implement Join Screen
The Join Screen will work as a medium to either schedule a new meeting or join an existing meeting.
Step 3: Initialize and Join Meeting
Using the provided token
and meetingId
, we will configure and initialize the meeting in viewDidLoad()
.
Then, we'll add @IBOutlet for localParticipantVideoView
and remoteParticipantVideoView
, which can render local and remote participant media, respectively.
Step 4: Implement Controls
After initializing the meeting in the previous step, we will now add @IBOutlet for btnLeave
, btnToggleVideo
and btnToggleMic
which can control the media in the meeting.
Step 5: Implementing MeetingEventListener
MeetingEventListener
In this step, we'll create an extension of the MeetingViewController
that implements the MeetingEventListener, which implements the onMeetingJoined
, onMeetingLeft
, onParticipantJoined
, onParticipantLeft
, onParticipantChanged
etc. methods.
We'll create another extension of the MeetingViewController
that implements the PubSubMessageListener
that triggers when you receive a new message, it implements onMessageReceived
method.
//Added pub-sub message listener.
Step 6: Implementing ParticipantEventListener
ParticipantEventListener
In this stage, we'll add an extension for the MeetingViewController
that implements the ParticipantEventListener, which implements the onStreamEnabled
and onStreamDisabled
methods for the audio and video of MediaStreams enabled or disabled.
The function update UI is frequently used to control or modify the user interface (enable/disable camera & mic) by the MediaStream state.
class MeetingViewController: UIViewController {
...
extension MeetingViewController: ParticipantEventListener {
/// Participant has enabled mic, video or screenshare
/// - Parameters:
/// - stream: enabled stream object
/// - participant: participant object
func onStreamEnabled(_ stream: MediaStream, forParticipant participant: Participant) {
updateUI(participant: participant, forStream: stream, enabled: true)
}
/// Participant has disabled mic, video or screenshare
/// - Parameters:
/// - stream: disabled stream object
/// - participant: participant object
func onStreamDisabled(_ stream: MediaStream,
forParticipant participant: Participant) {
updateUI(participant: participant, forStream: stream, enabled: false)
}
}
private extension MeetingViewController {
func updateUI(participant: Participant, forStream stream: MediaStream, enabled: Bool) { // true
switch stream.kind {
case .state(value: .video):
if let videotrack = stream.track as? RTCVideoTrack {
if enabled {
DispatchQueue.main.async {
UIView.animate(withDuration: 0.5){
if(participant.isLocal) {
self.localParticipantViewContainer.isHidden = false
self.localParticipantVideoView.isHidden = false
self.localParticipantVideoView.videoContentMode = .scaleAspectFill self.localParticipantViewContainer.bringSubviewToFront(self.localParticipantVideoView)
videotrack.add(self.localParticipantVideoView)
self.lblLocalParticipantNoMedia.isHidden = true
} else {
self.remoteParticipantViewContainer.isHidden = false
self.remoteParticipantVideoView.isHidden = false
self.remoteParticipantVideoView.videoContentMode = .scaleAspectFill
self.remoteParticipantViewContainer.bringSubviewToFront(self.remoteParticipantVideoView)
videotrack.add(self.remoteParticipantVideoView)
self.lblRemoteParticipantNoMedia.isHidden = true
}
}
}
} else {
UIView.animate(withDuration: 0.5){
if(participant.isLocal){
self.localParticipantViewContainer.isHidden = false
self.localParticipantVideoView.isHidden = true
self.lblLocalParticipantNoMedia.isHidden = false
videotrack.remove(self.localParticipantVideoView)
} else {
self.remoteParticipantViewContainer.isHidden = false
self.remoteParticipantVideoView.isHidden = true
self.lblRemoteParticipantNoMedia.isHidden = false
videotrack.remove(self.remoteParticipantVideoView)
}
}
}
}
case .state(value: .audio):
if participant.isLocal {
localParticipantViewContainer.layer.borderWidth = 4.0
localParticipantViewContainer.layer.borderColor = enabled ? UIColor.clear.cgColor : UIColor.red.cgColor
} else {
remoteParticipantViewContainer.layer.borderWidth = 4.0
remoteParticipantViewContainer.layer.borderColor = enabled ? UIColor.clear.cgColor : UIColor.red.cgColor
}
default:
break
}
}
}
...
Known Issue
Please add the following line to the MeetingViewController.swift
file's viewDidLoad
method If you get your video out of the container, view like below image.
TIP:
Stuck anywhere? Check out this example code on GitHub
After successfully integrating the VideoSDK into your iOS app, you've unlocked the power of high-quality video calling. This not only improves user experience but can encourage longer call duration and deeper engagement in your app. By adding a chat feature, you provide users with more flexibility and control during their calls.
Integrate Chat Feature in Video App
For communication or any kind of messaging between the participants, VideoSDK provides pubSub
classes that use the Publish-Subscribe mechanism and can be used to develop a wide variety of functionalities. For example, participants could use it to send chat messages to each other, share files or other media, or even trigger actions like muting or unmuting audio or video.
Now we will see, how we can use PubSub to implement Chat functionality. If you are not familiar with the PubSub mechanism and pubSub
class, you can follow this guide.
Implementing Group Chat
- The first step in creating a group chat is choosing the topic that all the participants will publish and subscribe to send and receive the messages. We will be using
CHAT
as the topic for this one. - On the send button, publish the message that the sender typed in the
Text
field.
NOTE:
It is assumed that you have created a user interface along with the implementation of the meeting class and its event listeners. For reference checkout our iOS example app on how can you setup your app.
class MeetingViewController {
//Button that will send the message when tapped
@IBAction func sendMessageTapped(_ sender: Any) {
let options = ["persist" : true]
// publish message
self.meeting?.pubsub.publish(topic: "CHAT", message: "How are you?", options: options)
}
}
- The next step would be to display the messages others send. For this, we have to
subscribe
to that topic i.eCHAT
and display all the messages. When a message is received, theonMessageReceived
event of thePubSubMessageListener
is triggered.
extension MeetingViewController: MeetingEventListener {
func onMeetingJoined() {
//subscribe to the topic 'CHAT' when onMeetingJoined is triggered
meeting?.pubsub.subscribe(topic: "CHAT", forListener: self)
}
}
extension MeetingViewController: PubSubMessageListener {
// read message when it is received
func onMessageReceived(_ message: PubSubMessage) {
print("Message Received: " + message.message)
}
}
The final step in the group chat would be unsubscribe
to that topic, which you had previously subscribed to but no longer needed. Here we are unsubscribe
to CHAT
topic on activity destroy.
extension MeetingViewController: MeetingEventListener {
func onMeetingLeft() {
//unsubscribe to the topic 'CHAT' when onMeetingLeft is triggered
////highlight-next-line
meeting?.pubsub.unsubscribe(topic: "CHAT", forListener: self)
}
}
- If you want to full guide about all features with chat, you can check our documentation:
Integrate Private Chat
In the above example, if you want to convert into a private chat between two participants, then all you have to do is pass the participantID of the participant in the sendOnly
option as ["sendOnly" : ["ABCD"]]
. Here sendOnly accepts an array of participant IDs if you wish to send a message to.
class MeetingViewController {
//Button that will send the private message when tapped
@IBAction func sendPrivateMessageTapped(_ sender: Any) {
//adding sendOnly option
let options = ["sendOnly" : ["ABCD"]]
// publish message
self.meeting?.pubsub.publish(topic: "CHAT", message: "How are you?", options: options)
}
}
Downloading Chat Messages
All the messages from the PubSub which where published with persist : true
and can be downloaded as a .csv
file. This file will be available in the VideoSDK dashboard as well as through the Sessions API.
Conclusion
By integrating chat functionality using PubSub, you've added a valuable layer of communication to your video call application with VideoSDK.live. This guide has equipped you with the knowledge to implement this valuable addition to your iOS app. This integration improves engagement and fosters better communication among users. By leveraging PubSub, developers can create a robust and scalable chat feature that complements the video calling experience.
Unlock the full potential of VideoSDK today and craft seamless video experiences! Sign up now to receive 10,000 free minutes and take your video app to new heights.