Skip to main content
Version: 1.x.x

PubSub - Android

PubSub feature allows the participant to send and receive messages of the topics which he has subscribed.

Methods

publish()

This method is use for publishing message of specific topic.

Syntax

public void publish(String topic, String message, PubSubPublishOptions options)
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.
optionsPubSubPublishOptionsThis is an object of PubSubPublishOptions, which provides an option, such as persist, which persists message history for upcoming participants.

Example

private fun sendMessage() {
val options = PubSubPublishOptions()
options.isPersist = true
meeting!!.pubSub.publish("CHAT", "Hello from Android", options)
}

subscribe()

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

Syntax

public List<PubSubMessage> subscribe(String topic, PubSubMessageListener listener)
Parameter NameTypeDescription
topicStringThis should be the topic to be subscribed.
listenerPubSubMessageListenerThis is an object of PubSubMessageListener, which listens for upcoming messages and calls onMessageReceived function, when new message received.

Example

val pubSubMessageListener = object : PubSubMessageListener {
override fun onMessageReceived(message: PubSubMessage) {
Log.d("#message", "onMessageReceived: ${message.message}")
}
override fun onOldMessagesReceived(messages: List<PubSubMessage>) {
// Persisted message list
Log.d("#message", "onOldMessagesReceived: $messages")
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
//...

// Subscribe for 'CHAT' topic
meeting!!.pubSub.subscribe("CHAT", pubSubMessageListener)

}

unsubscribe()

This method is used to unsubscribe the message topic.

Syntax

public void unsubscribe(String topic, PubSubMessageListener listener)
Parameter NameTypeDescription
topicStringThis should be the topic to be unsubscribed.
listenerPubSubMessageListenerThis is an object of PubSubMessageListener, which was passed in subscribe().

Example

fun unsubscribe() {
// Unsubscribe 'CHAT' topic
meeting!!.pubSub.unsubscribe("CHAT", pubSubMessageListener)
}

Sample Code

package live.videosdk.rtc.android.kotlin

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import live.videosdk.rtc.android.Meeting
import live.videosdk.rtc.android.listeners.PubSubMessageListener
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>) {
// 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.
*
* For Example: (MainActivity.kt)
* var meeting = VideoSDK.initMeeting(context, meetingId, ParticipantName, micEnabled, webcamEnabled, paricipantId, mode, customTrack, metaData,signalingBaseUrl,preferredProtocol)
* (this.application as MainApplication).meeting = meeting
*/

// Get Meeting
meeting = (this.application as MainApplication).meeting

// Subscribe for 'CHAT' topic
meeting!!.pubSub.subscribe("CHAT", pubSubMessageListener)

sendMessage()
}

private fun sendMessage() {
val options = PubSubPublishOptions()
options.isPersist = true
meeting!!.pubSub.publish("CHAT", "Hello from Android", options)
}

override fun onDestroy() {
// Unsubscribe for 'CHAT' topic
meeting!!.pubSub.unsubscribe("CHAT", pubSubMessageListener)
super.onDestroy()
}
}

Got a Question? Ask us on discord