TencentCloudChat.TYPES.USER_STATUS_ONLINE
): The current user has logged in and can receive and send messages.TencentCloudChat.TYPES.USER_STATUS_OFFLINE
): The offline status will not be triggered when the SDK for web is logged in/out. It will be triggered in the application with the IM SDK for React Native.TencentCloudChat.TYPES.USER_STATUS_UNLOGINED
): The user hasn't logged in since registration or has called logout
to log out.setSelfStatus
API to set a user's own custom status through the customStatus
field. Then, notifications of a change in the user's own custom status will be received through the TencentCloudChat.EVENT.USER_STATUS_UPDATED
event.setSelfStatus
API, you can leave the customStatus
field empty to clear the status.setSelfStatus
, you don't need to upgrade to the Premium edition or enable the feature in the console.chat.setSelfStatus(options);
options
parameter is of the Object
type. It contains the following attribute values:Name | Type | Description |
customStatus | String | Custom user status |
Promise
// Set `customStatus` to the empty string `''` to clear the user's own custom statuslet promise = chat.setSelfStatus({customStatus: 'xxx'});promise.then(function(imResponse) {console.log(imResponse.data);const { userID, statusType, customStatus } = imResponse.data;// userID - User ID// statusType - User status. The enumerated values are described as follows:// TencentCloudChat.TYPES.USER_STATUS_UNKNOWN - Unknown// TencentCloudChat.TYPES.USER_STATUS_ONLINE - Online// TencentCloudChat.TYPES.USER_STATUS_OFFLINE - Offline// TencentCloudChat.TYPES.USER_STATUS_UNLOGINED - Not logged in// customStatus - Custom user status}).catch(function(imError) {console.warn('setSelfStatus error:', imError); // Failed to set the user's own custom status});
getUserStatus
API to query the status of the user or another user. The API will return the general status and custom status of the queried user.getUserStatus
is called. In this case, this API can be called 20 times every five seconds, and the statuses of up to 500 users can be queried at a time.chat.getUserStatus(options);
options
parameter is of the Object
type. It contains the following attribute values:Name | Type | Description |
userIDList | Array | List of the userID values to be queried. Users only need to pass in their own userID to query their own status. |
Promise
// Query the user's own status// When `userIDList` contains only the user's own `userID`// it indicates to query only the user's own status.let promise = chat.getUserStatus({userIDList: [`${myUserID}`]});promise.then(function(imResponse) {const { successUserList } = imResponse.data;successUserList.forEach((item) => {const { userID, statusType, customStatus } = item;// userID - User ID// statusType - User status. The enumerated values are described as follows:// TencentCloudChat.TYPES.USER_STATUS_UNKNOWN - Unknown// TencentCloudChat.TYPES.USER_STATUS_ONLINE - Online// TencentCloudChat.TYPES.USER_STATUS_OFFLINE - Offline// TencentCloudChat.TYPES.USER_STATUS_UNLOGINED - Not logged in// customStatus - Custom user status});}).catch(function(imError) {console.warn('getUserStatus error:', imError); // Failed to obtain the user status});
// Query the status of another userlet promise = chat.getUserStatus({userIDList: ['user0', 'user1']});promise.then(function(imResponse) {const { successUserList, failureUserList } = imResponse.data;// List of `userID` values of the users whose statuses were queried successfullysuccessUserList.forEach((item) => {const { userID, statusType, customStatus } = item;// userID - User ID// statusType - User status. The enumerated values are described as follows:// TencentCloudChat.TYPES.USER_STATUS_UNKNOWN - Unknown// TencentCloudChat.TYPES.USER_STATUS_ONLINE - Online// TencentCloudChat.TYPES.USER_STATUS_OFFLINE - Offline// TencentCloudChat.TYPES.USER_STATUS_UNLOGINED - Not logged in// customStatus - Custom user status});// List of `userID` values of the users whose statuses failed to be queriedfailureUserList.forEach((item) => {const { userID, code, message } = item;// userID - `userID` of the user whose status failed to be queried// code - Error code for the failed query// message - Error message for the failed query});}).catch(function(imError) {console.warn('getUserStatus error:', imError); // Failed to obtain the user status});
subscribeUserStatus
API to subscribe to the status of the specified user. When the user status (including general status and custom status) subscribed to changes, the status change notification can be received through the TencentCloudChat.EVENT.USER_STATUS_UPDATED
event.subscribeUserStatus
is called.TencentCloudChat.EVENT.USER_STATUS_UPDATED
callback.chat.subscribeUserStatus(options);
options
parameter is of the Object
type. It contains the following attribute values:Name | Type | Description |
userIDList | Array | List of userID values. The number of userID values cannot exceed 100 per request. |
Promise
let promise = chat.subscribeUserStatus({userIDList: ['user0', 'user1']});promise.then(function(imResponse) {const { failureUserList } = imResponse.data;// List of `userID` values of the users whose statuses failed to be subscribed tofailureUserList.forEach((item) => {const { userID, code, message } = item;// userID - `userID` of the user whose status failed to be queried// code - Error code for the failed query// message - Error message for the failed query});}).catch(function(imError) {console.warn('subscribeUserStatus error:', imError); // Failed to subscribe to the user status});
unsubscribeUserStatus
API to unsubscribe from the user status or clear the subscription list.
If you don't want the subscription list to be cleared manually, after the account is logged out, the Chat backend will clear it after a certain period of time by default.unsubscribeUserStatus
is called.chat.unsubscribeUserStatus(options);
options
parameter is undefined
, it indicates to cancel all the current subscriptions. When it is of the Object
type, it contains the following attribute values:Name | Type | Description |
userIDList | Array | List of userID values. The number of userID values cannot exceed 100 per request. When userIDList is an empty array or undefined , it indicates to cancel all the current subscriptions. |
Promise
// Unsubscribe from some of the current user statuseslet promise = chat.unsubscribeUserStatus({userIDList: ['user0', 'user1']});promise.then(function(imResponse) {const { failureUserList } = imResponse.data;// List of `userID` values of the users whose statuses failed to be unsubscribed fromfailureUserList.forEach((item) => {const { userID, code, message } = item;// userID - `userID` of the user whose status failed to be queried// code - Error code for the failed query// message - Error message for the failed query});}).catch(function(imError) {console.warn('unsubscribeUserStatus error:', imError); // Failed to unsubscribe from the user status});
// Unsubscribe from all the current subscribed user statuseslet promise = chat.unsubscribeUserStatus();promise.then(function(imResponse) {const { failureUserList } = imResponse.data;// List of `userID` values of the users whose statuses failed to be unsubscribed fromfailureUserList.forEach((item) => {const { userID, code, message } = item;// userID - `userID` of the user whose status failed to be queried// code - Error code for the failed query// message - Error message for the failed query});}).catch(function(imError) {console.warn('unsubscribeUserStatus error:', imError); // Failed to unsubscribe from the user status});
TencentCloudChat.EVENT.USER_STATUS_UPDATED
event to notify a status change of any of the three types.TencentCloudChat.EVENT.USER_STATUS_UPDATED
callback, they are triggered in different ways for different user types.TencentCloudChat.EVENT.USER_STATUS_UPDATED
event listener, when a user's own status changes, the SDK will deliver the TencentCloudChat.EVENT.USER_STATUS_UPDATED
event, where the user can get the latest own status.TencentCloudChat.EVENT.USER_STATUS_UPDATED
event, where the latest status of the friend can be obtained.subscribeUserStatus
to subscribe to friends' statuses. When a friend's status changes, the SDK will deliver the TencentCloudChat.EVENT.USER_STATUS_UPDATED
callback.subscribeUserStatus
to subscribe to friends' statuses, changes in friends' statuses cannot be obtained.subscribeUserStatus
to subscribe to the status. When the user's status changes, the TencentCloudChat.EVENT.USER_STATUS_UPDATED
callback will be triggered, where the user can get the latest status of the non-friend user.let onUserStatusUpdated = function(event) {console.log(event.data);const userStatusList = event.data;userStatusList.forEach((item) => {const { userID, statusType, customStatus } = item;// userID - User ID// statusType - User status. The enumerated values are described as follows://TencentCloudChat
.TYPES.USER_STATUS_UNKNOWN - Unknown//TencentCloudChat
.TYPES.USER_STATUS_ONLINE - Online//TencentCloudChat
.TYPES.USER_STATUS_OFFLINE - Offline//TencentCloudChat
.TYPES.USER_STATUS_UNLOGINED - Not logged in// customStatus - Custom user status})};chat.on(TencentCloudChat
.EVENT.USER_STATUS_UPDATED, onUserStatusUpdated);
Was this page helpful?