subscribe

suspend fun subscribe(topic: String, listener: PubSubMessageListener, options: PubSubSubscribeOptions? = null)

Subscribes to a topic; listener receives every message published on it.

The topic's persisted messages are replayed right after subscribing, in server-paced batches, through PubSubMessageListener.onOldMessagesReceived with a PubSubHistoryInfo whose isLast is true on the final batch. A topic with no history fires no history callback at all.

Requires a joined room. Before join completes it throws and RoomEventListener.onError fires with 3035 (ERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINED), whose message names this method; while the room is RoomState.RECONNECTING it throws 3065 (ERROR_ROOM_RECONNECTING) without reporting it on onError. Rejected calls are not replayed once the room reconnects.

Suspends until the server has confirmed the subscription.

Parameters

options

bounds how much history is replayed (PubSubSubscribeOptions.oldMessageLimit) and how live messages are paced under load (PubSubSubscribeOptions.realtimeOverflow, with drops reported on PubSubMessageListener.onMessageDrop); null replays the history in full

Throws

3073 (ERROR_INVALID_PARAMETER) if the options are invalid — a negative oldMessageLimit, or maxQueue combined with RealtimeOverflow.DROP — or if this listener is already subscribed to this topic; both are checked before the room state. 3035 / 3065 if the room is not joined or is reconnecting, and 4088 (PUBSUB_SUBSCRIBE_FAILED, also reported on onError) when the subscription fails, in which case nothing is registered — retry once the room is RoomState.CONNECTED