PubSub - Android
The PubSub feature allows participants to send and receive messages on the topics they have subscribed to.
Methods
publish(), subscribe() and unsubscribe() are regular functions, not suspend functions. They throw VideoSDKException when a call is refused, for example before the meeting is joined or while it is reconnecting.
publish()
This method is used to publish a message to a specific topic.
Syntax
fun publish(topic: String, message: String, options: PubSubPublishOptions? = null, payload: JSONObject? = null)
| Parameter Name | Type | Description |
|---|---|---|
| topic | String | This should be the topic for which you are publishing a message. |
| message | String | This is the actual message, which will be published to participants, who had subscribed to a particular topic. |
| options | PubSubPublishOptions? | This is an object of PubSubPublishOptions, which provides an option, such as persist, which persists message history for upcoming participants. |
| payload | JSONObject? | Optional data sent along with the message. |
Example
private fun sendMessage() {
val options = PubSubPublishOptions()
options.setPersist(true)
try {
meeting!!.pubSub.publish("CHAT", "Hello from Android", options)
} catch (e: VideoSDKException) {
Log.e("#message", "publish failed: ${e.code} ${e.name}")
}
}
subscribe()
This method is used to subscribe to a particular topic. Messages which were sent earlier with persist set arrive on the listener's onOldMessagesReceived(), in one or more batches.
Syntax
fun subscribe(topic: String, listener: PubSubMessageListener, options: PubSubSubscribeOptions? = null)
| Parameter Name | Type | Description |
|---|---|---|
| topic | String | This should be the topic to be subscribed. |
| listener | PubSubMessageListener | This is an object of PubSubMessageListener, which listens for upcoming messages and calls onMessageReceived function, when new message received. |
| options | PubSubSubscribeOptions? | Optional. Limits how many old messages are delivered, for example with setOldMessageLimit(). |
Example
val pubSubMessageListener = object : PubSubMessageListener {
override fun onMessageReceived(message: PubSubMessage) {
Log.d("#message", "onMessageReceived: ${message.message}")
}
override fun onOldMessagesReceived(messages: List<PubSubMessage>, info: PubSubHistoryInfo) {
// Persisted message list, in batches. info.isLast marks the last batch.
Log.d("#message", "onOldMessagesReceived: $messages")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
//...
// Subscribe for 'CHAT' topic
try {
meeting!!.pubSub.subscribe("CHAT", pubSubMessageListener)
} catch (e: VideoSDKException) {
Log.e("#message", "subscribe failed: ${e.code} ${e.name}")
}
}
unsubscribe()
This method is used to unsubscribe from a topic.
Syntax
fun unsubscribe(topic: String, listener: PubSubMessageListener)
| Parameter Name | Type | Description |
|---|---|---|
| topic | String | This should be the topic to be unsubscribed. |
| listener | PubSubMessageListener | This is an object of PubSubMessageListener, which was passed in subscribe(). |
Example
fun unsubscribe() {
// Unsubscribe 'CHAT' topic
try {
meeting!!.pubSub.unsubscribe("CHAT", pubSubMessageListener)
} catch (e: VideoSDKException) {
Log.e("#message", "unsubscribe failed: ${e.code} ${e.name}")
}
}
Sample Code
package live.videosdk.rtc.android.kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import live.videosdk.rtc.android.Meeting
import live.videosdk.rtc.android.lib.PubSubMessage
import live.videosdk.rtc.android.lib.VideoSDKException
import live.videosdk.rtc.android.listeners.PubSubMessageListener
import live.videosdk.rtc.android.model.PubSubHistoryInfo
import live.videosdk.rtc.android.model.PubSubPublishOptions
class ChatActivity : AppCompatActivity() {
// Meeting
var meeting: Meeting? = null
// PubSubMessageListener
val pubSubMessageListener = object : PubSubMessageListener {
override fun onMessageReceived(message: PubSubMessage) {
Log.d("#message", "onMessageReceived: ${message.message}")
}
override fun onOldMessagesReceived(messages: List<PubSubMessage>, info: PubSubHistoryInfo) {
// Persisted message list
Log.d("#message", "onOldMessagesReceived: $messages")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat)
/**
* Here, we have created 'MainApplication' class, which extends android.app.Application class.
* It has Meeting property and getter and setter methods of Meeting property.
* In your android manifest, you must declare the class implementing android.app.Application (add the android:name=".MainApplication" attribute to the existing application tag):
* In MainActivity.kt, we have set Meeting property, and joined the meeting.
*
* For Example: (MainActivity.kt)
* val meeting = VideoSDK.initMeeting(context, meetingId, participantName, micEnabled, webcamEnabled, participantId, mode, multiStream, customTracks, metaData)
* (this.application as MainApplication).meeting = meeting
*/
// Get Meeting
meeting = (this.application as MainApplication).meeting
try {
// Subscribe for 'CHAT' topic
meeting!!.pubSub.subscribe("CHAT", pubSubMessageListener)
sendMessage()
} catch (e: VideoSDKException) {
Log.e("#message", "PubSub call failed: ${e.code} ${e.name}")
}
}
private fun sendMessage() {
val options = PubSubPublishOptions()
options.setPersist(true)
meeting!!.pubSub.publish("CHAT", "Hello from Android", options)
}
override fun onDestroy() {
try {
// Unsubscribe for 'CHAT' topic
meeting!!.pubSub.unsubscribe("CHAT", pubSubMessageListener)
} catch (e: VideoSDKException) {
Log.e("#message", "unsubscribe failed: ${e.code} ${e.name}")
}
super.onDestroy()
}
}
Got a Question? Ask us on discord

