Skip to main content
Version: 4.x.x

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​

  1. 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 CHAT as the topic for this one.
  2. On the send button, publish the message that the sender typed in the text field.
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 = ""
}
}
  1. Next step would be to display the messages. For this we have to subscribe to that topic i.e CHAT. Each new message arrives in onMessageReceived(_:), on the main queue. Messages that were published with persist: true before you subscribed arrive in onOldMessagesReceived(_:info:).
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
}
}
  1. The final step is to unsubscribe from that topic once you no longer need it. Here we unsubscribe from the CHAT topic when the chat closes. Call it before leave(): once you have left, unsubscribe() fails with ERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINED.
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.

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