Chat rooms
Chat rooms suit temporary, live scenarios such as live streams: there's no fixed membership, users enter when they open the screen and leave when they leave it, and messages only go to people in the room at that moment, with no offline messages. That's what sets them apart from group chats. For member limits, message retention and priority rules, see Chat room management in the server docs.
| You want to | See |
|---|---|
| Show the room list, enter a room | List and enter |
| Show and send messages, likes | State and messages |
| Room attributes such as seats, recall, member list | Attributes, recall and members |
| Moderators muting, kicking, muting everyone | Management |
List and enter
val page = im.chatrooms.list(namePrefix = "直播")
val room = im.chatrooms.enter(roomId, leaveOthers = false)
println(room.snapshot.value.state)list/getquery rooms,onlineCountsgets online counts for several rooms, andcreatecreates a room from the client (if the app allows it).entertakes an optional stringext, sent to others with the entry notification.leaveOthers = truealso leaves other rooms you're in, for switching rooms.enteredis the rooms currently entered, as aStateFlowof aMap.
State and messages
import com.deeprespond.im.*
room.send(OutgoingContent.Text("大家好"), priority = ChatroomPriority.NORMAL)
room.like()
room.snapshot.collect { snapshot ->
println(snapshot.messages.size)
}The snapshot has info, attributes, attributesVersion, messages, state and removed. messages keeps up to 500 messages in arrival order. state is one of:
state | Meaning |
|---|---|
entering | Entering |
in | In the room |
reentering | The SDK is re-entering automatically after a disconnect |
removed | Removed, banned, or the room was dissolved. Tell the user based on removed.reason, and don't keep re-entering |
left | Left |
Events: room.events / on deliver MessageReceived, MessageRecalled, MemberJoined, MemberLeft, Like, Dropped and Removed. Do the collection above in a coroutine tied to the screen's lifecycle.
Priority (priority):
HIGHis only for the owner and admins;NORMALis regular chat;LOWis for likes, effects and other messages that are fine to lose: it isn't retried and may be dropped when the room is busy. The exact rate-limit rules are decided by the server.- Chat room messages don't use the regular send queue, so nothing waits to be sent during a disconnect.
Attributes, recall and members
setAttributes(attributes, autoDelete, force)sets attributes (such as seats in a voice room), andremoveAttributesremoves them. Some keys may fail, so checkAttributeResult.failedkey by key.- Attributes set with
autoDelete = trueare deleted automatically when the person who set them leaves.forceoverwrites keys set by others and is only for the owner and admins. recall(messageId)recalls a chat room message,memberspages through the people in the room right now, andrefreshfetches the room information again.- Attributes and member events are for display only; permissions are still checked by the server.
Management
Call these through room.manage:
| Function | Method |
|---|---|
| Name, announcement, capacity and other information | update(ChatroomPatch(...), version?) |
| Mute everyone, admins, owner | setMuteAll, setAdmin, transferOwner |
| Mute, ban, kick | mute, unmute, ban, unban, kick |
| Allowlist | allowlist.list/add/remove |
| View lists | mutes, bans |
| Dissolve | dismiss |
- Batch operations such as mute, ban and kick return a result per person; check
code/okfor each. - Duration parameters (
duration) are KotlinDurations. - Call
room.leave()when leaving the screen to actually leave the room; just cancelling the state collection doesn't leave it.
For the full API, see ChatroomsModule and ChatroomHandle.
