Skip to main content
Version: 3.x.x

Chat using PubSub - iOS

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
Task {
do {
try await self.meeting?.pubsub.publish(topic: "CHAT", message: "How are you?", options: options)
} catch {
print("error while publish: \(error)")
}
}

}
}
  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
Task {
var demoOptions = PubSubSubscribeOptions()
demoOptions.oldMessageLimit = 10
demoOptions.realtimeOverflow = .queue
demoOptions.maxQueue = 30

do {
try await meeting?.pubsub.subscribe(
topic: "CHAT",
forListener: self,
options: demoOptions
)
} catch {
print("\(topic) subscribe failed: \(error.localizedDescription)")
}
}
}
}
extension MeetingViewController: PubSubMessageListener {
// read message when it is received
func onMessageReceived(_ message: PubSubMessage) {
print("Message Received: " + message.message)
}

func onOldMessagesReceived(_ messages: [VideoSDKRTC.PubSubMessage], info: PubSubHistoryInfo) {
print("onOldMessagesReceived: \(messages.count) | isLast: \(info.isLast)")
}

func onBatchReceived(_ messages: [VideoSDKRTC.PubSubMessage]) {
print("Batch messages: \(messages.count)")
}

func onMessageDrop(_ info: PubSubMessageDropInfo) {
print("\(info.droppedCount) message(s) dropped")
}
}
  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
Task {
do {
try await self.meeting?.pubsub.unsubscribe(topic: "CHAT", forListener: self)
} catch {
print("unsubscribe failed: \(error)")
}
}
}
}

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
Task {
do {
try await self.meeting?.pubsub.publish(topic: "CHAT", message: "How are you?", options: options)
} catch {
print("error while publish: \(error)")
}
}
}
}

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