Skip to main content
Version: 2.0.x

Chat during Live Stream - iOS

Enhance your live stream experience by enabling real-time audience chat using VideoSDK's usePubSub hook. 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 class, this guide will walk you through the necessary steps.

Implementing Chat​

Group Chat(Public Chat for All Viewers)​

To allow your entire audience to chat during the stream:

  1. Participants need to subscribe to a common topic, such as "CHAT", to receive incoming messages.
import UIKit
import VideoSDKRTC

class GroupChatViewController: UIViewController, PubSubMessageListener {
var meeting: Meeting!
var messages: [PubSubMessage] = []

override func viewDidLoad() {
super.viewDidLoad()
// Subscribing to the "CHAT" topic
meeting.pubSub.subscribe(topic: "CHAT", forListener: self)
}

// 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 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.

  1. Participants can send messages by publishing to the "CHAT" topic. Setting persist to true ensures that messages are retained for participants who join later.
@IBAction func sendMessageTapped(_ sender: UIButton) {
let messageText = "Hello, everyone!"
let options = ["persist": true]
// Publishing the message to the "CHAT" topic
meeting.pubSub.publish(topic: "CHAT", message: messageText, options: options)
}

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 = ["sendOnly": [recipientId]]
meeting.pubSub.publish(topic: "CHAT", message: messageText, options: options)
}

Display Latest Message Notificaiton​

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 an .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