Skip to main content
Version: 4.x.x

Chat using PubSub - iOS

Implementing Chat​

Group Chat​

  1. The first step in creating a group chat is choosing the topic that all participants publish and subscribe to. We will use CHAT as the topic.
  2. On the send button, publish the message that the sender typed in the Text field.
note

It is assumed that you have created a user interface along with the implementation of the Room class and its event listeners. For reference, see our iOS example app for how to set up your app. The example uses iOS SDK 3.x; the Migration Guide lists what changed in 4.0.0.

extension RoomViewController {
//Button that will send the message when tapped
@IBAction func sendMessageTapped(_ sender: Any) {
let options = PubSubPublishOptions(persist: true)
// publish message
Task {
do {
try await self.room?.pubSub.publish(topic: "CHAT", message: "How are you?", options: options)
} catch {
print("error while publish: \(error)")
}
}

}
}
  1. Next step would be to display the messages others send. For this we have to subscribe to that topic i.e CHAT and display all the messages. When a message is received, the onMessageReceived event of the PubSubMessageListener is triggered.
extension RoomViewController: RoomEventListener {
func onRoomJoined() {
//subscribe to the topic 'CHAT' when onRoomJoined is triggered
Task {
var demoOptions = PubSubSubscribeOptions()
demoOptions.oldMessageLimit = 10
demoOptions.realtimeOverflow = .QUEUE
demoOptions.maxQueue = 30

do {
try await room?.pubSub.subscribe(
topic: "CHAT",
listener: self,
options: demoOptions
)
} catch {
print("CHAT subscribe failed: \(error)")
}
}
}
}
extension RoomViewController: 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")
}
}
  1. The final step is to unsubscribe from the topic when you no longer need it. Here we unsubscribe from the CHAT topic when the user closes the chat. Do this before you leave the room. After you leave, unsubscribe() can fail with ERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINED.
extension RoomViewController {
//Button that will close the chat when tapped
@IBAction func closeChatTapped(_ sender: Any) {
//unsubscribe from the topic 'CHAT' when the chat is no longer needed
Task {
do {
try await self.room?.pubSub.unsubscribe(topic: "CHAT", listener: 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 PubSubPublishOptions(sendOnly: ["ABCD"]). Here sendOnly accepts array of participant IDs you wish to send message to.

extension RoomViewController {
//Button that will send the private message when tapped
@IBAction func sendPrivateMessageTapped(_ sender: Any) {
//adding sendOnly option
let options = PubSubPublishOptions(sendOnly: ["ABCD"])
// publish message
Task {
do {
try await self.room?.pubSub.publish(topic: "CHAT", message: "How are you?", options: options)
} catch {
print("error while publish: \(error)")
}
}
}
}

Downloading Chat Messages​

All PubSub messages published with persist: true can be downloaded as a .csv file. This file is available in the VideoSDK dashboard and 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