# Room
The `Room` interface represents an individual chat room instance. It provides access to all chat features such as messages, presence, reactions, typing indicators, and occupancy. Rooms are the primary way users interact with chat functionality.
## Obtaining a room instance
Use the [`rooms.get()`](https://ably.com/docs/chat/api/kotlin/rooms.md#get) method to obtain a room instance:
### Kotlin
```
val room = chatClient.rooms.get("my-room")
```
For more information, see the [rooms documentation](https://ably.com/docs/chat/rooms.md).
## Properties
The `Room` interface has the following properties:
### Kotlin
```
room.attach()
```
### Returns
`Unit`
This is a suspend function. It completes when the room is attached, or throws a [`ChatException`](https://ably.com/docs/chat/api/kotlin/chat-client.md#chatexception) on failure.
For more information, see [attach to a room](https://ably.com/docs/chat/rooms.md#attach).
## Detach from a room
`suspend room.detach(): Unit`
Detach from the room to stop receiving messages and events. Existing subscriptions are preserved but will not receive events until the room is re-attached.
### Kotlin
```
room.detach()
```
### Returns
`Unit`
This is a suspend function. It completes when the room is detached, or throws a [`ChatException`](https://ably.com/docs/chat/api/kotlin/chat-client.md#chatexception) on failure.
For more information, see [detach from a room](https://ably.com/docs/chat/rooms.md#detach).
## Subscribe to room status changes
`room.onStatusChange(listener: RoomStatusListener): Subscription`
Register a listener to receive room status change events. The listener is called whenever the room transitions between lifecycle states.
### Kotlin
```
val subscription = room.onStatusChange { change ->
println("Room status changed to: ${change.current}")
change.error?.let { error ->
println("Error: $error")
}
}
// To remove the listener
subscription.unsubscribe()
```
### Parameters
The `onStatusChange()` method takes the following parameters:
### Kotlin
```
val subscription = room.onDiscontinuity { reason ->
println("Discontinuity detected: $reason")
// You may want to re-fetch recent messages here
}
// To remove the listener
subscription.unsubscribe()
```
### Parameters
The `onDiscontinuity()` method takes the following parameters:
### Kotlin
```
import kotlinx.coroutines.launch
launch {
room.statusAsFlow().collect { change ->
println("Room status: ${change.current}")
}
}
```
### Returns
`Flow
### Kotlin
```
import kotlinx.coroutines.launch
launch {
room.discontinuityAsFlow().collect { error ->
println("Discontinuity detected: $error")
}
}
```
### Returns
`Flow
### Kotlin
```
import com.ably.chat.RoomStatus
import com.ably.chat.statusAsFlow
import com.ably.chat.discontinuityAsFlow
import kotlinx.coroutines.launch
val room = chatClient.rooms.get("my-room")
// Attach to start receiving events
room.attach()
// Monitor room status with a listener
val statusSub = room.onStatusChange { change ->
println("Room status: ${change.current}")
}
// Monitor for discontinuities
val discSub = room.onDiscontinuity { reason ->
println("Discontinuity detected, consider re-fetching messages")
}
// Or use Flow for reactive collection
launch {
room.statusAsFlow().collect { change ->
when (change.current) {
RoomStatus.Attached -> println("Room attached")
RoomStatus.Suspended -> println("Room suspended")
RoomStatus.Failed -> println("Room failed: ${change.error}")
else -> {}
}
}
}
launch {
room.discontinuityAsFlow().collect { error ->
println("Discontinuity: $error")
}
}
// Access room features
val messages = room.messages
val presence = room.presence
val typing = room.typing
// Get the room name
println("Room name: ${room.name}")
// When done, detach and clean up
room.detach()
statusSub.unsubscribe()
discSub.unsubscribe()
```
## Related Topics
- [ChatClient](https://ably.com/docs/chat/api/kotlin/chat-client.md): API reference for the ChatClient interface in the Ably Chat Kotlin SDK.
- [Connection](https://ably.com/docs/chat/api/kotlin/connection.md): API reference for the Connection interface in the Ably Chat Kotlin SDK.
- [Rooms](https://ably.com/docs/chat/api/kotlin/rooms.md): API reference for the Rooms interface in the Ably Chat Kotlin SDK.
- [Messages](https://ably.com/docs/chat/api/kotlin/messages.md): API reference for the Messages interface in the Ably Chat Kotlin SDK.
- [Message](https://ably.com/docs/chat/api/kotlin/message.md): API reference for the Message interface in the Ably Chat Kotlin SDK.
- [MessageReactions](https://ably.com/docs/chat/api/kotlin/message-reactions.md): API reference for the MessageReactions interface in the Ably Chat Kotlin SDK.
- [Presence](https://ably.com/docs/chat/api/kotlin/presence.md): API reference for the Presence interface in the Ably Chat Kotlin SDK.
- [Occupancy](https://ably.com/docs/chat/api/kotlin/occupancy.md): API reference for the Occupancy interface in the Ably Chat Kotlin SDK.
- [Typing](https://ably.com/docs/chat/api/kotlin/typing.md): API reference for the Typing interface in the Ably Chat Kotlin SDK.
- [RoomReactions](https://ably.com/docs/chat/api/kotlin/room-reactions.md): API reference for the RoomReactions interface in the Ably Chat Kotlin SDK.
## Documentation Index
To discover additional Ably documentation:
1. Fetch [llms.txt](https://ably.com/llms.txt) for the canonical list of available pages.
2. Identify relevant URLs from that index.
3. Fetch target pages as needed.
Avoid using assumed or outdated documentation paths.