11/*
2- * Copyright 2023-2024 LiveKit, Inc.
2+ * Copyright 2023-2025 LiveKit, Inc.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -104,6 +104,10 @@ private val DEFAULT_ERROR_HANDLER: ((Room, Exception?) -> Unit) = { _, e ->
104104 * @param onDisconnected a listener to be called upon room disconnection.
105105 * @param onError a listener to be called upon room error.
106106 * @param passedRoom if a [Room] is provided, it will be used. If null, a new Room will be created instead.
107+ * @param disconnectOnDispose by default, this composable handles the connection management and will disconnect the room
108+ * when the composable goes out of scope. Setting this to false will disable this behavior. This is effective in combination
109+ * with [passedRoom], when you need to keep the [Room] object alive and connected separately from the UI
110+ * (for example, with a background service).
107111 */
108112@Composable
109113fun rememberLiveKitRoom (
@@ -119,6 +123,7 @@ fun rememberLiveKitRoom(
119123 onDisconnected : (suspend CoroutineScope .(Room ) -> Unit )? = null,
120124 onError : ((Room , Exception ? ) -> Unit )? = DEFAULT_ERROR_HANDLER ,
121125 passedRoom : Room ? = null,
126+ disconnectOnDispose : Boolean = true,
122127): Room {
123128 val context = LocalContext .current
124129 val room = remember(passedRoom) {
@@ -210,7 +215,7 @@ fun rememberLiveKitRoom(
210215
211216 DisposableEffect (room, connect) {
212217 onDispose {
213- if (connect) {
218+ if (connect && disconnectOnDispose ) {
214219 room.disconnect()
215220 }
216221 }
@@ -242,6 +247,10 @@ fun rememberLiveKitRoom(
242247 * @param onDisconnected a listener to be called upon room disconnection.
243248 * @param onError a listener to be called upon room error.
244249 * @param passedRoom if a [Room] is provided, it will be used. If null, a new Room will be created instead.
250+ * @param disconnectOnDispose by default, this composable handles the connection management and will disconnect the room
251+ * when the composable goes out of scope. Setting this to false will disable this behavior. This is effective in combination
252+ * with [passedRoom], when you need to keep the [Room] object alive and connected separately from the UI
253+ * (for example, with a background service).
245254 */
246255@Composable
247256fun RoomScope (
@@ -257,6 +266,7 @@ fun RoomScope(
257266 onDisconnected : (suspend CoroutineScope .(Room ) -> Unit )? = null,
258267 onError : ((Room , Exception ? ) -> Unit )? = null,
259268 passedRoom : Room ? = null,
269+ disconnectOnDispose : Boolean = true,
260270 content : @Composable (room: Room ) -> Unit
261271) {
262272 val room = rememberLiveKitRoom(
@@ -271,7 +281,8 @@ fun RoomScope(
271281 onConnected = onConnected,
272282 onDisconnected = onDisconnected,
273283 onError = onError,
274- passedRoom = passedRoom
284+ passedRoom = passedRoom,
285+ disconnectOnDispose = disconnectOnDispose,
275286 )
276287
277288 CompositionLocalProvider (
0 commit comments