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.
createRoom(roomInfo: ZIMRoomInfo, config: ZIMRoomAdvancedConfig): Promise<ZIMRoomCreatedResult>
- 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 ofKey
is 16 bytes, the maximum length ofValue
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.
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.
});
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.
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);
});
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
.
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);
});
Get room properties
var roomID = '';
zim.queryRoomAllAttributes(roomID)
.then(function ({ roomID, roomAttributes }) {
// Query successful.
})
.catch(function (err) {
// Query failed.
});
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.
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);
});