logo
On this page

Manage room properties


ZEGOCLOUD's In-app Chat (the ZIM SDK) provides the capability of room property management, allowing you to customize the properties of a specified room.

With this feature, you can build your app to meet different requirements of various scenarios: customize the in-room properties in a live audio streaming or live chatroom, manage your speaker seats in a live audio room, mark player role and the latest facts and figures in an online Werewolf and other online board games.

Set room properties

To set room properties, set them when you call the createRoom method to create a room.

Untitled
createRoom(roomInfo: ZIMRoomInfo, config: ZIMRoomAdvancedConfig): Promise<ZIMRoomCreatedResult>
1
Copied!
Warning
  • Up to 20 room properties can be set in a room. The room property is stored in the format of Key-Value. The maximum length of Key is 16 bytes, the maximum length of Value is 1024 bytes, and the total length of all properties must not exceed 5120 bytes. For more customizable room properties, contact ZEGOCLOUD Technical Support.
  • The customized room properties you set will be cleared after the room is destroyed.
Sample code
var roomInfo = { roomID: '', roomName: '' };
var config = { 
    roomAttributes: { key1: 'value1', key2: 'value2' }
};

zim.createRoom(roomInfo, config)
    .then(function ({ roomInfo }) {
        // Operation successful.
    })
    .catch(function (err) {
        // Operation failed.
    });
1
Copied!

Modify room properties

The room properties you set are stored as key-value.

  • When Key does not exist: Setting room property means adding room property.
  • When the Key already exists: Setting the room property means updating the value of the existing room property.
Sample code
var roomID = '';
var roomAttributes = { key1: 'value1', key2: 'value2' };
var config = { 
    isForce: false,
    isUpdateOwner: false,
    isDeleteAfterOwnerLeft: false
};

zim.setRoomAttributes(roomAttributes, roomID, config)
    .then(function ({ roomID, errorKeys }) {
        // Operation successful.
    })
    .catch(function (err) {
        // Operation failed.
    });

// Set up the callback roomAttributesUpdated.
zim.on('roomAttributesUpdated', function (zim, { roomID, infos }) {
    console.log(''roomAttributesUpdated'', roomID, infos);
});
1
Copied!

Delete room properties

Generally, you can only delete the room properties that you own. You can also delete the room properties created by others by setting the isForce value in config.

Sample code
var roomID = '';
var keys = ['key1'];
var config = { 
    isForce: false
};

zim.deleteRoomAttributes(keys, roomID, config)
    .then(function ({ roomID, errorKeys }) {
        // Operation successful.
    })
    .catch(function (err) {
        // Operation failed.
    });

// Set up the callback roomAttributesUpdated.
zim.on('roomAttributesUpdated', function (zim, { roomID, infos }) {
    console.log(''roomAttributesUpdated'', roomID, infos);
});
1
Copied!

Get room properties

Sample code
var roomID = '';
zim.queryRoomAllAttributes(roomID)
    .then(function ({ roomID, roomAttributes }) {
        // Query successful.
    })
    .catch(function (err) {
        // Query failed.
    });
1
Copied!

Combined room properties operation

The merge operation means that you can combine multiple operations within the same room into one operation using the beginRoomAttributesBatchOperation and endRoomAttributesBatchOperation methods. This is typically used when you want to perform consecutive operations without being interrupted by other users' operations.

Sample code
var roomID = '';
var config = { 
    isForce: false,
    isUpdateOwner: false,
    isDeleteAfterOwnerLeft: false
};

// Start a combined room properties operation.
zim.beginRoomAttributesBatchOperation(roomID, config);

// Use the `Promise.then` to get the operation result. Do not use the `async-await`.
var roomAttributes = { key1: 'value1', key2: 'value2' };
zim.setRoomAttributes(roomAttributes, roomID, config)
   .then(function ({ roomID, errorKeys }) {
        // Operation successful.
    })
    .catch(function (err) {
        // Operation failed.
    });

// Use the `Promise.then` to get the operation result. Do not use the `async-await`.
var keys = ['key1'];
zim.deleteRoomAttributes(keys, roomID, config)
   .then(function ({ roomID, errorKeys }) {
        // Operation successful.
    })
    .catch(function (err) {
        // Operation failed.
    });

// End the operation. 
zim.endRoomAttributesBatchOperation(roomID)
    .then(function ({ roomID }) {
        // Operation successful.
    })
    .catch(function (err) {
        // Operation failed.
    });

// Set up the callback roomAttributesBatchUpdated.
zim.on('roomAttributesBatchUpdated', function (zim, { roomID, infos }) {
    console.log(''roomAttributesBatchUpdated'', roomID, infos);
});
1
Copied!

Previous

Manage room info

Next

Manage room user attributes