# Manage chat rooms (/en/realtime-media/im/build/build-groups-rooms-and-threads/chat-room/manage-chatrooms/unity)

> For AI agents: see the complete documentation index at [llms.txt](/llms.txt).

Chat rooms enable real-time messaging among multiple users.

    A chat room does not have a strict membership, and members do not retain any permanent relationship with each other. Once going offline, chat room members cannot receive any messages from the chat room and automatically leave the chat room after 2 minutes (members on the chat room allow list remain in the chat room even if they stay offline for 2 minutes or more). A chat room is widely applied in live broadcast use cases such as stream chat in Twitch.

    This page shows how to use the Chat SDK to create and manage a chat room in your app.

    ## Understand the tech [#understand-the-tech-5]

    The Chat SDK provides the`Room`, `IRoomManager`, and `IRoomManagerDelegate` classes for chat room management, which allow you to implement the following features:

    ## Prerequisites [#prerequisites-5]

    Before proceeding, ensure that you meet the following requirements:

    * You have initialized the Chat SDK. For details, see [SDK quickstart](../../../get-started-sdk).
    * You understand the call frequency limit of the Chat APIs supported by different pricing plans as described in [Limitations](../../limitations).
    * You understand the number of chat rooms supported by different pricing plans as described in [Pricing Plan Details](../../../reference/pricing-plan-details).
    * Only the app super admin has the privilege of creating a chat room. Ensure that you have added an app super admin by calling the [super-admin RESTful API](..//en/api-reference/api-ref/im/chatroom-management/manage-chatroom-admins#adding-a-chat-room-super-admin).

    ## Implementation [#implementation-5]

    This section introduces how to call the APIs provided by the Chat SDK to implement the features listed above.

    ### Create a chat room [#create-a-chat-room-3]

    Only the [app super admin](..//en/api-reference/api-ref/im/chatroom-management/manage-chatroom-admins) can call `CreateRoom` to create a chat room and set the chat room attributes such as the chat room name, description, and maximum number of members. Once a chat room is created, the super admin automatically becomes the chat room owner.

    <CalloutContainer type="info">
      <CalloutDescription>
        You are advised to call the [RESTful API](/en/api-reference/api-ref/im/chatroom-management/manage-chatrooms#creating-a-chat-room) to create a chat room from the server.
      </CalloutDescription>
    </CalloutContainer>

    The following code sample shows how to create a chat room:

    ```csharp
    SDKClient.Instance.RoomManager.CreateRoom(subject, description, welcomeMsg, maxUserCount, members, callback: new ValueCallBack(
      onSuccess: (room) => {
      },
      onError:(code, desc) => {
      }
    ));
    ```

    ### Destroy a chat room [#destroy-a-chat-room-3]

    Only the chat room owner can call `DestroyRoom` to disband a chat room. Once a chat room is disbanded, all chat room members receive the `OnDestroyedFromRoom` callback and are immediately removed from the chat room.

    The following code sample shows how to destroy a chat room:

    ```csharp
    SDKClient.Instance.RoomManager.DestroyRoom(roomId, new CallBack(
      onSuccess: () => {
      },
      onError: (code, desc) => {
      }
    ));
    ```

    ### Join a chat room [#join-a-chat-room-3]

    Refer to the following steps to join a chat room:

    1. Call `FetchPublicRoomsFromServer` to retrieve the list of chat rooms from the server and locate the ID of the chat room that you want to join.

    2. Call `JoinRoom` to pass in the chat room ID and join the specified chat room. Once a user joins a chat room, all the other chat room members receive the `OnMemberJoinedFromRoom` callback.

    The following code sample shows how to join a chat room:

    ```csharp
    // Retrieve the list of chat rooms from the server
    SDKClient.Instance.RoomManager.FetchPublicRoomsFromServer(callback: new ValueCallBack>(
                // `result` is of PageResult type
                onSuccess: (result) => {
                },
                onError:(code, desc) => {
                }
            ));

    // Join a chat room
    SDKClient.Instance.RoomManager.JoinRoom(roomId, new ValueCallBack(
      onSuccess: (room) => {
      },
      onError: (code, desc) => {
      }
    ));
    ```

    ### Leave a chat room [#leave-a-chat-room-3]

    All chat room members can call `LeaveRoom` to leave the specified chat room. Once a member leaves the chat room, all the other chat room members receive the `OnMemberExitedFromRoom` callback.

    **Note**: Unlike chat group owners (who cannot leave their groups), a chat room owner can leave a chat room. After re-entering the chat room, this user remains the chat room owner.

    The following code sample shows how to leave a chat room:

    ```csharp
    SDKClient.Instance.RoomManager.LeaveRoom(roomId, new CallBack(
      onSuccess: () => {
      },
      onError: (code, desc) => {
      }
    ));
    ```

    By default, after a user leaves a chat room, the Chat SDK removes all chat room messages on the local device. If you do not want these messages removed, set `Options#DeleteMessagesAsExitRoom` to `false` when initializing the SDK.

    The following code sample shows how to retain the chat room messages after leaving a chat room:

    ```csharp
    Options options = new Options();
    options. DeleteMessagesAsExitRoom = false;
    ```

    ### Update the chat room member count in real time [#update-the-chat-room-member-count-in-real-time-5]

    If many members join or leave a chat room within a short period, you can update the member count in real time:

    1. When a user joins a chat room, other members receive the `OnMemberJoinedFromRoom` event. Similarly, when a member leaves or is removed, other members receive the `OnMemberExitedFromRoom` or `OnRemovedFromRoom` event.

    2. After receiving an event, call `RoomManager#GetChatRoom` to retrieve local chat room details, and refer to `RoomManager#MemberCount` for the current number of members.

       ```csharp
       class RoomManagerDelegate : IRoomManagerDelegate
       {
           public void OnMemberJoinedFromRoom(string roomId, string participant, string ext)
           {
               int memberCount = SDKClient.Instance.RoomManager.GetChatRoom(roomId).MemberCount;
           }

           public void OnMemberExitedFromRoom(string roomId, string roomName, string participant)
           {
           }
           // Implement other functions of IRoomManagerDelegate
       }

       // Register the delegate
       RoomManagerDelegate roomManagerDelegate = new RoomManagerDelegate();
       SDKClient.Instance.RoomManager.AddRoomManagerDelegate(roomManagerDelegate);
       ```

    ### Retrieve the chat room attributes [#retrieve-the-chat-room-attributes-3]

    All chat room members can call `FetchRoomInfoFromServer` to retrieve the attributes of the chat room, including the chat room ID, name, description, announcement, owner, admin list, member list, block list, mute list, maximum number of members, and whether all members are muted.

    The following code sample shows how to retrieve the chat room attributes:

    ```csharp
    SDKClient.Instance.RoomManager.FetchRoomInfoFromServer(roomId, new ValueCallBack(
      onSuccess: (room) => {
      },
      onError: (code, desc) => {
      }
    ));
    ```

    ### Retrieve the chat room list from the server [#retrieve-the-chat-room-list-from-the-server-3]

    Users can call `FetchPublicRoomsFromServer` to get the chat room list from the server. You can get a maximum of 1,000 chat rooms at each call.

    ```csharp
    // You can set the value of `pageSize` to a maximum of 1000.
    SDKClient.Instance.RoomManager.FetchPublicRoomsFromServer(pageNum, pageSize, callback: new ValueCallBack>(
      // `rooms` is of PageResult type.
      onSuccess: (rooms) => {
      },
      onError:(code, desc) => {
      }
    ));
    ```

    ### Listen for chat room events [#listen-for-chat-room-events-5]

    To monitor the chat room events, you can listen for the callbacks in the `IRoomManagerDelegate` class and add app logics accordingly. If you want to stop listening for the callback, make sure that you remove the listener to prevent memory leakage.

    The following code sample shows how to add and remove the chat room listener:

    ```csharp
    // Inherits and implements the IRoomManagerDelegate class.
    public class RoomManagerDelegate : IRoomManagerDelegate {
        ......
        public void OnDestroyedFromRoom(string roomId, string roomName)
        {
        }
        ......
    }
    // Adds the chat room listener.
    RoomManagerDelegate adelegate = new RoomManagerDelegate();
    SDKClient.Instance.RoomManager.AddRoomManagerDelegate(adelegate);

    // Removes the chat room listener.
    SDKClient.Instance.RoomManager.AddRoomManagerDelegate(adelegate);
    ```

    Refer to the following code sample to listen for chat room events:

    ```csharp
    public interface IRoomManagerDelegate
        {
            /**
            * Occurs when a chat room instance is destroyed.
            *
            * @param roomId        The chat room ID
            * @param roomName      The chat room name
            *
            */
            void OnDestroyedFromRoom(string roomId, string roomName);
            /**
            * Occurs when a user joins a chat room.
            *
            * @param roomId        The chat room ID
            * @param participant   The user ID of the new chat room member
            *
            */
            void OnMemberJoinedFromRoom(string roomId, string participant);
            /**
            * Occurs when a member leaves a chat room.
            *
            * @param roomId        The chat room ID
            * @param roomName      The chat room name
            * @param participant   The user ID of the member who leaves the chat room
            *
            */
            void OnMemberExitedFromRoom(string roomId, string roomName, string participant);
            /**
            * Occurs when a member is removed from a chat room.
            *
            * @param roomId        The chat room ID
            * @param roomName      The chat room name
            * @param participant   The user ID of the member who is removed from the chat room
            *
            */
            void OnRemovedFromRoom(string roomId, string roomName, string participant);
            /**
            * Occurs when one or more members are added to the chat room mute list. The muted members receive this event.
            * Available since SDK v1.4.0.
            *
            * @param roomId    The chat room ID
            * @param mutes     A dictionary that maps each muted member's user ID to their mute expiration timestamp (a Unix timestamp in milliseconds)
            */
            void OnMuteListAddedFromRoom(string roomId, Dictionary<string, long> mutes);
            /**
            * Occurs when a member is added to the chat room mute list.
            * Deprecated since SDK v1.4.0. Use OnMuteListAddedFromRoom(string, Dictionary<string, long>) instead.
            *
            * @param roomId        The chat room ID
            * @param mutes         The user IDs of the members added to the chat room mute list
            * @param expireTime    The Unix timestamp when the mute duration expires, in milliseconds
            */
            void OnMuteListAddedFromRoom(string roomId, List mutes, long expireTime);
            /**
            * Occurs when a member is removed from the chat room mute list.
            *
            * @param roomId        The chat room ID
            * @param mutes         The user IDs of the members removed from the chat room mute list
            *
            */
            void OnMuteListRemovedFromRoom(string roomId, List mutes);
            /**
            * Occurs when a member is promoted to a chat room admin.
            *
            * @param roomId        The chat room ID
            * @param admin         The user ID of the member promoted to an admin
            *
            */
            void OnAdminAddedFromRoom(string roomId, string admin);
            /**
            * Occurs when an admin is demoted to a chat room member.
            *
            * @param  roomId       The chat room ID
            * @param  admin        The user ID of the admin demoted to a member
            *
            */
            void OnAdminRemovedFromRoom(string roomId, string admin);
            /**
            * Occurs when the chat room ownership is transferred.
            *
            * @param roomId        The chat room ID
            * @param newOwner      The user ID of the new chat room owner
            * @param oldOwner      The user ID of the former chat room owner
            *
            */
            void OnOwnerChangedFromRoom(string roomId, string newOwner, string oldOwner);
            /**
            * Occurs when the chat room announcement is updated.
            * @param roomId        The chat room ID
            * @param announcement  The updated chat room announcement.
            *
            */
            void OnAnnouncementChangedFromRoom(string roomId, string announcement);
            /**
             * Occurs when the custom chat room attributes (key-value maps) are updated.
             *
             * @param roomId        The chat room ID
             * @param kv            The updated attributes
             * @param from          The user ID of the operator
             */
            void OnChatroomAttributesChanged(string roomId, Dictionary kv, string from);
             /**
             * Occurs when the custom chat room attributes (key-value maps) are removed.
             *
             * @param roomId        The chat room ID.
             * @param keys          The keys of removed attributes
             * @param from          The user ID of the operator
             */
            void OnChatroomAttributesRemoved(string roomId, List keys, string from);
            /**
                  * Occurs when the chat room member(s) is/are added to the allow list.
            *
            * @param roomId The chat room ID.
            * @param members  The member(s) added to the allow list.
            */
            void OnAddAllowListMembersFromChatroom(string roomId, List members);
                  /**
            * Occurs when the chat room member(s) is/are removed from the allow list.
            *
            * @param roomId The chat room ID.
            * @param members  The member(s) removed from the allow list.
            */
            void OnRemoveAllowListMembersFromChatroom(string roomId, List members);
                  /**
            * Occurs when all members in the chat room are muted or unmuted.
            *
            * @param roomId The chat room ID.
            * @param isAllMuted    Whether all chat room members are muted.
            */
            void OnAllMemberMuteChangedFromChatroom(string roomId, bool isAllMuted);
                  /**
            * Occurs when the chat room specifications are changed.
            *
            * @param room The chat room instance.
            */
                  void OnSpecificationChangedFromRoom(Room room);
        }
    ```

    
  
      
  
