Realtime
Subscribe to a collection
Section titled “Subscribe to a collection”import com.bunbase.sdk.SubscribeOptions
data class Post(val title: String, val body: String)val posts = client.collection("posts", Post::class.java)
val sub = client.realtime.subscribe<Post>(posts.channel) { event -> when (event.event) { RealtimeEventType.create -> println("New: ${event.record.title}") RealtimeEventType.update -> println("Updated: ${event.record.title}") RealtimeEventType.delete -> println("Deleted") }}
// Unsubscribe when done (e.g., in onCleared)sub.cancel()Subscription is automatically cancelled when garbage-collected, but always call cancel() explicitly in lifecycle methods.
Subscribe to a single record
Section titled “Subscribe to a single record”val sub = client.realtime.subscribe<Post>(posts.recordChannel("post_id")) { event -> println("Record changed: ${event.record}")}Filter by event type
Section titled “Filter by event type”val sub = client.realtime.subscribe<Post>( channel = posts.channel, options = SubscribeOptions(events = listOf(RealtimeEventType.create)),) { event -> println("New post: ${event.record.title}")}Filter by field value
Section titled “Filter by field value”val sub = client.realtime.subscribe<Post>( channel = posts.channel, options = SubscribeOptions(filter = mapOf("status" to "published")),) { event -> // Only events where post.status == "published"}Subscribe to current user’s records
Section titled “Subscribe to current user’s records”val sub = client.realtime.subscribe<Post>(posts.mineChannel) { event -> // Only records owned by the authenticated user}Subscribe to multiple records by ID
Section titled “Subscribe to multiple records by ID”val sub = client.realtime.subscribe<Post>( channel = posts.recordsChannel, options = SubscribeOptions(ids = listOf("id_1", "id_2", "id_3")),) { event -> // events for any of the listed IDs}lifecycleScope.launch { try { client.realtime.ping() println("Connected") } catch (e: BunBaseError.Timeout) { println("No response") }}Presence (requires Redis in the server)
Section titled “Presence (requires Redis in the server)”val userIds = client.realtime.presence("collection:chat")println("Online: $userIds")List active subscriptions
Section titled “List active subscriptions”val channels = client.realtime.getSubscriptions()println(channels)Lifecycle management
Section titled “Lifecycle management”class ChatViewModel : ViewModel() { private var sub: Subscription? = null
fun startListening() { sub = client.realtime.subscribe<Message>(channel) { event -> // Handle message } }
override fun onCleared() { sub?.cancel() }}Manual connect / disconnect
Section titled “Manual connect / disconnect”// Eagerly open the connection (auth events delivered immediately)client.realtime.connect()
// Close and stop reconnectingclient.realtime.disconnect()Auth revocation
Section titled “Auth revocation”When the server pushes a session_revoked or account_deleted event, the SDK automatically:
- Clears tokens from memory and
EncryptedSharedPreferences. - Emits
nullonauth.currentUser. - Closes the WebSocket.
No extra setup required.
Reconnect backoff
Section titled “Reconnect backoff”The SDK reconnects automatically on unexpected close with exponential backoff: 500 ms, 1 s, 2 s, … up to 30 s.