PubSub Class Methods - Android
publish()
publish()is used to publish messages on a specified topic in the meeting.
Parameters
-
topic
- type:
String - This is the name of the topic, for which message will be published.
- type:
-
message
- type:
String - This is the actual message.
- type:
-
options
- type:
PubSubPublishOptions - This specifies the options for publish.
- type:
-
payload
- type:
JSONObject OPTIONAL- If you need to include additional information along with a message, you can pass here as
JSONObject.
- type:
Returns
void
Example
- Kotlin
- Java
// Publish message for 'CHAT' topic
val publishOptions = PubSubPublishOptions()
publishOptions.isPersist = true
try {
meeting!!.pubSub.publish("CHAT", "Hello from Android", publishOptions)
} catch (e: Exception) {
// Meeting is not connected yet, or is reconnecting
Log.d("VideoSDK", "publish failed: ${e.message}")
}
// Publish message for 'CHAT' topic
PubSubPublishOptions publishOptions = new PubSubPublishOptions();
publishOptions.setPersist(true);
try {
meeting.pubSub.publish("CHAT", "Hello from Android", publishOptions);
} catch (Exception e) {
// Meeting is not connected yet, or is reconnecting
Log.d("VideoSDK", "publish failed: " + e.getMessage());
}
publish() requires a joined meeting. Calling it before the meeting reaches the CONNECTED state throws an IllegalStateException and fires onError with code 3001 (ERROR_ACTION_PERFORMED_BEFORE_MEETING_JOINED). While the meeting is RECONNECTING it throws without an error event.
If the message cannot be delivered — the server rejects it, or the connection drops before it is acknowledged — onError fires with code 4087 (PUBSUB_PUBLISH_FAILED) carrying the reason.
subscribe()
subscribe()is used to subscribe a particular topic to get all the messages of that particular topic in the meeting.
Parameters
-
topic:
- type:
String - Participants can listen to messages on that particular topic.
- type:
-
listener:
- type:
PubSubMessageListener
- type:
-
options:
- type:
PubSubSubscribeOptions OPTIONAL- Controls history limit, live-message pacing and overflow behaviour. Passing
nullbehaves exactly like the two-parameter overload.
- type:
Returns
void
Example
- Kotlin
- Java
val pubSubMessageListener = object : PubSubMessageListener {
override fun onMessageReceived(message: PubSubMessage) {
Log.d("#message", "onMessageReceived: ${message.message}")
}
override fun onOldMessagesReceived(messages: List<PubSubMessage>) {
Log.d("#message", "onOldMessagesReceived: $messages")
}
}
try {
// Subscribe for 'CHAT' topic
meeting!!.pubSub.subscribe("CHAT", pubSubMessageListener)
// Or, subscribe with batched delivery options
val options = PubSubSubscribeOptions()
options.oldMessageLimit = 100
meeting!!.pubSub.subscribe("CHAT", pubSubMessageListener, options)
} catch (e: IllegalArgumentException) {
// Invalid options, or this listener is already subscribed to the topic
Log.d("VideoSDK", "subscribe rejected: ${e.message}")
} catch (e: Exception) {
// Meeting is not connected yet, or is reconnecting
Log.d("VideoSDK", "subscribe failed: ${e.message}")
}
PubSubMessageListener pubSubMessageListener = new PubSubMessageListener() {
@Override
public void onMessageReceived(PubSubMessage message) {
Log.d("#message", "onMessageReceived: " + message.getMessage());
}
@Override
public void onOldMessagesReceived(List<PubSubMessage> messages) {
Log.d("#message", "onOldMessagesReceived: " + messages);
}
};
try {
// Subscribe for 'CHAT' topic
meeting.pubSub.subscribe("CHAT", pubSubMessageListener);
// Or, subscribe with batched delivery options
PubSubSubscribeOptions options = new PubSubSubscribeOptions();
options.setOldMessageLimit(100);
meeting.pubSub.subscribe("CHAT", pubSubMessageListener, options);
} catch (IllegalArgumentException e) {
// Invalid options, or this listener is already subscribed to the topic
Log.d("VideoSDK", "subscribe rejected: " + e.getMessage());
} catch (Exception e) {
// Meeting is not connected yet, or is reconnecting
Log.d("VideoSDK", "subscribe failed: " + e.getMessage());
}
subscribe() requires a joined meeting. Calling it before the meeting reaches the CONNECTED state throws an IllegalStateException and fires onError with code 3001 (ERROR_ACTION_PERFORMED_BEFORE_MEETING_JOINED). While the meeting is RECONNECTING it throws without an error event.
If the server rejects the subscription, the listener is unregistered again and onError fires with code 4088 (PUBSUB_SUBSCRIBE_FAILED). Invalid options, or subscribing the same listener to the same topic twice, throw an IllegalArgumentException.
unsubscribe()
unsubscribe()is used to unsubscribe a particular topic on which you have subscribed priviously.
Parameters
-
topic:
- type:
String - This is the name of the topic to be unsubscribed.
- type:
-
listener:
- type:
PubSubMessageListener
- type:
Returns
void
Example
- Kotlin
- Java
// Unsubscribe for 'CHAT' topic
try {
meeting!!.pubSub.unsubscribe("CHAT", pubSubMessageListener)
} catch (e: Exception) {
// Meeting already ended or is reconnecting — nothing left to unsubscribe from
Log.d("VideoSDK", "unsubscribe skipped: ${e.message}")
}
// Unsubscribe for 'CHAT' topic
try {
meeting.pubSub.unsubscribe("CHAT", pubSubMessageListener);
} catch (Exception e) {
// Meeting already ended or is reconnecting — nothing left to unsubscribe from
Log.d("VideoSDK", "unsubscribe skipped: " + e.getMessage());
}
The topic itself is unsubscribed only once its last listener is removed. Removing that last listener requires a joined meeting: before the meeting reaches CONNECTED it throws an IllegalStateException and fires onError with code 3001; while the meeting is RECONNECTING it throws without an error event. Removing any other listener is local and never throws.
If the server rejects the unsubscribe, onError fires with code 4089 (PUBSUB_UNSUBSCRIBE_FAILED); the listener is removed locally either way.
Got a Question? Ask us on discord

