Skip to main content
Version: 0.0.x

PubSub (BETA)

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

var pubSubMessageListener =
PubSubMessageListener { message ->
// Logs new message
Log.v("New Message Received", "Message : " + message.message)
}

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

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

// Logs persisted message list
for (message in pubSubMessageList) {
Log.v("Message List: ", "Message : " + message.message)
}
}

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
var pubSubMessageListener =
PubSubMessageListener { message ->
// Logs new message
Log.v("New Message Received", "Message : " + message.message)
}

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)
* (this.application as MainApplication).meeting = meeting
*/

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

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

for (message in pubSubMessageList) {
// Logs persisted messages
Log.v("Message List: ", "Message : " + message.message)
}

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()
}
}