Chat during Live Stream - iOS
Enhance your live stream experience by enabling real-time audience chat using VideoSDK's PubSub. Whether you’re streaming a webinar, online event, or an interactive session, integrating a chat system lets your viewers engage, ask questions, and react instantly. This guide shows how to build a group or private chat interface for a live stream using the Publish-Subscribe (PubSub) mechanism.
This guide focuses on implementing chat functionality in an iOS application using VideoSDK's PubSub mechanism. If you're new to the PubSub system and the pubSub property of the Room class, this guide will walk you through the necessary steps.
PubSub works only after the livestream is joined, so the code below expects room to be joined already. PubSub methods are async throws, so call them with try await inside a Task. PubSub lists the errors each method throws.
Implementing Chat
Group Chat(Public Chat for All Viewers)
To allow your entire audience to chat during the stream:
- Participants need to subscribe to a common topic, such as "CHAT", to receive incoming messages.
import UIKit
import VideoSDKRTC
class GroupChatViewController: UIViewController, PubSubMessageListener {
var room: Room!
var messages: [PubSubMessage] = []
override func viewDidLoad() {
super.viewDidLoad()
Task {
do {
// Subscribing to the "CHAT" topic
try await room.pubSub.subscribe(topic: "CHAT", listener: self)
} catch {
print("Error while subscribe: \(error)")
}
}
}
// Callback method when a new message is received
func onMessageReceived(_ message: PubSubMessage) {
messages.append(message)
DispatchQueue.main.async {
self.updateChatUI()
}
}
// Method to update the chat UI with new messages
func updateChatUI() {
// Implement UI update to display chat messages
}
}
In the above setup, the GroupChatViewController class conforms to the PubSubMessageListener protocol, allowing it to receive messages published to the "CHAT" topic. The onMessageReceived method appends incoming messages to the messages array and updates the chat UI accordingly.
- Participants can send messages by publishing to the "CHAT" topic. Setting
persisttotrueensures that messages are retained for participants who join later. Those participants receive the earlier messages inonOldMessagesReceived(_:info:), not inonMessageReceived(_:), so implement that callback too if you want to show the chat history.
@IBAction func sendMessageTapped(_ sender: UIButton) {
let messageText = "Hello, everyone!"
let options = PubSubPublishOptions(persist: true)
// Publishing the message to the "CHAT" topic
Task {
do {
try await room.pubSub.publish(topic: "CHAT", message: messageText, options: options)
} catch {
print("Error while publish: \(error)")
}
}
}
Private Chat (1:1 between Host and Viewer)
Private messaging is ideal when a host or moderator needs to directly respond to a viewer’s question. This can be achieved using the sendOnly property.
@IBAction func sendPrivateMessageTapped(_ sender: UIButton) {
let messageText = "Hello, this is a private message."
let recipientId = "participant_id_here" // Replace with actual participant ID
let options = PubSubPublishOptions(sendOnly: [recipientId])
Task {
do {
try await room.pubSub.publish(topic: "CHAT", message: messageText, options: options)
} catch {
print("Error while publish: \(error)")
}
}
}
Display Latest Message Notification
To alert hosts/moderators about new incoming chat messages in real-time:
func onMessageReceived(_ message: PubSubMessage) {
messages.append(message)
DispatchQueue.main.async {
self.updateChatUI()
self.showNewMessageAlert(message: message)
}
}
func showNewMessageAlert(message: PubSubMessage) {
let alert = UIAlertController(title: "New Message", message: "\(message.senderName) says: \(message.message)", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default))
self.present(alert, animated: true)
}
Downloading Chat Messages
All the messages from PubSub published with persist : true can be downloaded as a .csv file. This file will be accessible in the VideoSDK dashboard and through the Sessions API.
API Reference
The API references for all the methods and events utilized in this guide are provided below.
Got a Question? Ask us on discord

