Chat using PubSub - iOS
For the communication or any kind of messaging in between the participants, VideoSDK provides the PubSub class, available as room.pubSub, which uses 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 Chat
Group Chat
- The first step in creating a group chat is choosing the topic that all participants publish and subscribe to, to send and receive messages. We will be using
CHATas the topic for this one. - On the send button, publish the message that the sender typed in the text field.
- Swift
import UIKit
import VideoSDKRTC
class ChatViewController: UIViewController {
let room: Room
// Chat messages received so far
var messages: [PubSubMessage] = []
let messageField = UITextField()
init(room: Room) {
self.room = room
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// Action of the send button
@objc func sendTapped() {
guard let text = messageField.text, !text.isEmpty else { return }
Task {
do {
try await room.pubSub.publish(
topic: "CHAT",
message: text,
options: PubSubPublishOptions(persist: true)
)
} catch {
print("Error while sending message: \(error)")
}
}
messageField.text = ""
}
}
- Next step would be to display the messages. For this we have to
subscribeto that topic i.eCHAT. Each new message arrives inonMessageReceived(_:), on the main queue. Messages that were published withpersist: truebefore you subscribed arrive inonOldMessagesReceived(_:info:).
- Swift
extension ChatViewController: PubSubMessageListener {
// Call from viewDidLoad()
func subscribeToChat() {
Task {
do {
try await room.pubSub.subscribe(topic: "CHAT", listener: self)
} catch {
print("Error while subscribing to CHAT: \(error)")
}
}
}
func onMessageReceived(_ message: PubSubMessage) {
messages.append(message)
// reload your message list here
}
// Messages published with persist: true before you subscribed
func onOldMessagesReceived(_ messages: [PubSubMessage], info: PubSubHistoryInfo) {
self.messages.append(contentsOf: messages)
// reload your message list here
}
}
- The final step is to
unsubscribefrom that topic once you no longer need it. Here weunsubscribefrom theCHATtopic when the chat closes. Call it beforeleave(): once you have left,unsubscribe()fails withERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINED.
- Swift
extension ChatViewController {
// Call when the chat closes, before you leave the live stream
func unsubscribeFromChat() {
Task {
do {
try await room.pubSub.unsubscribe(topic: "CHAT", listener: self)
} catch {
print("Error while unsubscribing from CHAT: \(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 sendOnly parameter in options.
- Swift
extension ChatViewController {
func sendMessageTo(participant: Participant) {
let options = PubSubPublishOptions(persist: false, sendOnly: [participant.id])
Task {
do {
try await room.pubSub.publish(topic: "CHAT", message: "Hello!", options: options)
} catch {
print("Error while sending private message: \(error)")
}
}
}
}
Downloading Chat Messages
All the messages from the PubSub which were published with persist : true can be downloaded as a .csv file. This file will be available in the VideoSDK dashboard as well as through 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

