Skip to main content
Version: 2.0.x

Chat using PubSub - iOS

For the communication or any kind of messaging in between the participants, VideoSDK provides pubSub class which use Publish-Subscribe mechanism and can be used to develope wide varitey 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 Chat

Group Chat

  1. First step in creating a group chat is choosing the topic which all the participants will publish and subscribe to send and receive the messages. We will be using CHAT as the topic for this one.
  2. 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)
}
}
  1. Next step would be to display the messages others send. For this we have to subscribe to that topic i.e CHAT and display all the messages. When a message is received, the onMessageReceived event of the PubSubMessageListener 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)
}
}
  1. Final step in the group chat would be unsubscribe to that topic, which you had previously subscribed 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)
}
}

Private Chat

In the above example, if you want to convert into the 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 array of participant IDs you wish to send 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 an .csv file. This file will be available in the VideoSDK dashboard as well as throught the Sessions API.

API Reference

The API references for all the methods and events utilised in this guide are provided below.

Got a Question? Ask us on discord