Skip to main content
Version: 2.x.x

PubSubMessageListener Class - Android


onMessageReceived()

  • This event will be emitted whenever any pubsub message received.

  • Parameter: message — type PubSubMessage

onOldMessagesReceived()

  • This event will be emitted with one batch of the topic's persisted messages after subscribing.

  • Only messages published with persist enabled are persisted; a topic with none fires no history callback at all.

  • Parameter: messages — type List<PubSubMessage>

caution

History is replayed in server-paced batches, so this is called once per batch with a partial list — append the messages to what you already hold rather than replacing it. This method carries no end-of-replay signal; override onOldMessagesReceived(messages, info) when you need to know that history is complete.

onOldMessagesReceived(messages, info)

  • This event will be emitted for each batch of the topic's persisted messages after subscribing.

  • History streams in server-paced batches; the final batch carries info.isLast() as true.

  • Both subscribe(topic, listener) and the PubSubSubscribeOptions overload replay history this way — the options change only how much of it is replayed, never how it is delivered.

  • Parameters:

    • messages — type List<PubSubMessage>, the historical messages in this batch.
    • info — type PubSubHistoryInfo, batch metadata. See PubSubHistoryInfo.
  • If you do not override this method, the SDK forwards each batch to onOldMessagesReceived(messages), so a listener written against that method receives every message without overriding this one.

note

A topic with no history fires no history callback at all. Do not wait on isLast() to detect an empty topic.

onBatchReceived()

  • This event will be emitted once per live batch with all messages of that batch, before the per-message onMessageReceived() calls for the same batch.

  • Override this to amortise per-message work — a single database transaction or analytics flush per batch instead of one per message.

  • Parameter: messages — type List<PubSubMessage>, the live messages in this batch.

note

Batched delivery is enabled per meeting by the server configuration, not by subscribe options. Where it is not enabled this method is never called and live messages arrive solely through onMessageReceived() — handle both paths. Because the callback runs on the main thread, move the work itself off it.

onMessageDrop()

  • This event will be emitted when live messages were dropped because the subscriber's buffer overflowed under RealtimeOverflow.QUEUE.

  • Parameter: info — type PubSubMessageDropInfo. See PubSubMessageDropInfo.

  • It is coalesced — it fires once per drop event, not once per dropped message, and never fires under RealtimeOverflow.DROP.

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

override fun onOldMessagesReceived(messages: List<PubSubMessage>, info: PubSubHistoryInfo) {
Log.d("#message", "history batch of ${messages.size}, isLast: ${info.isLast}")
}

override fun onBatchReceived(messages: List<PubSubMessage>) {
Log.d("#message", "onBatchReceived: ${messages.size} messages")
}

override fun onMessageDrop(info: PubSubMessageDropInfo) {
Log.d("#message", "onMessageDrop: ${info.droppedCount} messages lost")
}
}

PubSubHistoryInfo

Metadata for a batch of historical messages delivered via onOldMessagesReceived(messages, info).

isLast()

  • type: boolean

  • Returns true on the final history batch, meaning the topic's history is complete.


PubSubMessageDropInfo

Details of a message-drop event delivered via onMessageDrop().

getDroppedCount()

  • type: int

  • Returns how many live messages were lost in this drop event. The event is coalesced, so one event can represent many dropped messages.

Got a Question? Ask us on discord