Chat using PubSub - iOS
Implementing Chat
Group Chat
- 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
CHATas the topic for this one. - On the send button, publish the message that the sender typed in the
Textfield.
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.
- Swift
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)")
}
}
}
}
- Next step would be to display the messages others send. For this we have to
subscribeto that topic i.eCHATand display all the messages. When a message is received, theonMessageReceivedevent of thePubSubMessageListeneris triggered.
- Swift
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")
}
}
- Final step in the group chat would be
unsubscribeto that topic, which you had previously subscribed but no longer needed. Here we areunsubscribetoCHATtopic on activity destroy.
- Swift
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.
- Swift
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

