Skip to main content
Version: 2.0.x

PubSub - iOS

PubSub is a short acronym for Publish-Subscribe mechanism. This mechanism is used to send and recieve messages from a particular topic. As the name suggests, for someone to send a message, they have to specify the topic and the message which should be published and for someone to receive a message, they should be subscribed to that topic.

Here is a visual to better understand publish-subscribe mechanism.

pubsub

Methods

publish()

This method is use for publishing message of specific topic.

Syntax

func publish(topic: String, message: String, options: [String : Any] = [:])
Parameter NameTypeDescription
topicStringThis should be the topic for which you are publishing a message.
messageStringThis is the actual message, which will be published to participants, who had subscribed to a particular topic.
optionsObjectThis is the object which provides an option, such as persist, which persists message history for upcoming participants.

Example

func sendMessage() {
meeting.pubsub.publish(topic: "CHAT", message: "Hello Everyone!", options: ["persist": true])
}

subscribe()

This method is used to subscribe for particular topic. This method returns a list of messages which were sent earlier.

Syntax

func subscribe(topic: String, forListener listener: PubSubMessageListener)
Parameter NameTypeDescription
topicStringThis should be the topic to be subscribed.
forListenerPubSubMessageListenerThis is an object of PubSubMessageListener, which listens for upcoming messages and calls onMessageReceived function, when new message received.

Example

class ChatMessageListener: PubSubMessageListener {
func onMessageReceived(_ message: PubSubMessage) {
// Print new message
print("Message Received:= " + message.message)
}
}
const listener = ChatMessageListener()

func subscribe() {
// Subscribe 'CHAT' topic
meeting?.pubsub.subscribe(topic: "CHAT", forListener: listener)
}

unsubscribe()

This method is used to unsubscribe the message topic.

Syntax

func unsubscribe(topic: String, forListener listener: PubSubMessageListener)
Parameter NameTypeDescription
topicStringThis should be the topic to be unsubscribed.
forListenerPubSubMessageListenerThis is an object of PubSubMessageListener, which was passed in subscribe().

Example

func unsubscribe() {
// Unsubscribe 'CHAT' topic
meeting?.pubsub.unsubscribe(topic: "CHAT", forListener: listener)
}

Sample Code

import UIKit
import VideoSDKRTC

class ChatViewController: UIViewController {

// Meeting
public var meeting: Meeting

// Chat Topic
var topic: String = "CHAT"

var chatMessageListener: ChatMessageListener = ChatMessageListener()

init(meeting: Meeting) {
self.meeting = meeting;

// Subscribe 'CHAT' topic
meeting.pubsub.subscribe(topic: topic, forListener: chatMessageListener)

// Get persisted chat messages
let pubsubMessages = meeting.pubsub.getMessagesForTopic(topic)

for msg in pubsubMessages! {
print("Message: " + msg.message)
}

super.init(nibName: nil, bundle: nil)
}

override func viewDidLoad() {
super.viewDidLoad()
}

required init?(coder: NSCoder) {
fatalError("Fatal Error")
}


// Lifecycle
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
meeting.pubsub.publish(topic: topic, message: "Hii from iOS", options: ["persist": true])
print("Chat Message Published")
}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
meeting.pubsub.unsubscribe(topic: topic, forListener: chatMessageListener)
print("Unsubscribed 'CHAT' topic")
}
}

class ChatMessageListener: PubSubMessageListener{
func onMessageReceived(_ message: PubSubMessage){
print("Message Received: " + message.message)
}
}

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