Skip to main content
Version: 2.x.x

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.
  • message

    • type: String
    • This is the actual message.
  • options

  • payload

    • type: JSONObject
    • OPTIONAL
    • If you need to include additional information along with a message, you can pass here as JSONObject.

Returns

  • void

Example

// 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}")
}
caution

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.
  • listener:

  • options:

    • type: PubSubSubscribeOptions
    • OPTIONAL
    • Controls history limit, live-message pacing and overflow behaviour. Passing null behaves exactly like the two-parameter overload.

Returns

  • void

Example

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}")
}
caution

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.
  • listener:

Returns

  • void

Example

// 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}")
}
caution

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