Retrieve Users
Retrieve Logged In User Details
You can get the details of the logged-in user using the getLoggedInUser()
method. This method can also be used to check if the user is logged in or not.
If the method returns Promise
with reject callback, it indicates that the user is not logged in and you need to log the user into CometChat SDK.
- Get Logged In User Details
- Typescript
var user = CometChat.getLoggedinUser().then(
user => {
console.log("user details:", { user });
}, error => {
console.log("error getting details:", { error });
}
);
CometChat.getLoggedinUser().then(
(user: CometChat.User) => {
console.log("user details:", { user });
}, (error: CometChat.CometChatException) => {
console.log("error getting details:", { error });
}
);
This method will return a User
object containing all the information related to the logged-in user.
Retrieve List of Users
In order to fetch the list of users, you can use the UsersRequest
class. To use this class i.e to create an object of the UsersRequest
class, you need to use the UsersRequestBuilder
class. The UsersRequestBuilder
class allows you to set the parameters based on which the users are to be fetched.
The UsersRequestBuilder
class allows you to set the below parameters:
Set Limit
This method sets the limit i.e. the number of users that should be fetched in a single iteration.
- Set Limit
- Typescript
let limit = 30;
let usersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.build();
let limit: number = 30;
let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.build();
Set Search Keyword
This method allows you to set the search string based on which the users are to be fetched.
- Set Search Keyword
- Typescript
let limit = 30;
let searchKeyword = "super";
let usersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.setSearchKeyword(searchKeyword)
.build();
let limit: number = 30;
let searchKeyword: string = "super";
let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.setSearchKeyword(searchKeyword)
.build();
Search In
This method allows you to define in which user property should the searchKeyword be searched. This method only works in combination with setSearchKeyword()
. By default the keyword is searched in both UID & Name.
- Javascript
- Typescript
let limit = 30;
let searchKeyword = "super";
let searchIn = ["uid", "name"];
let usersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.setSearchKeyword(searchKeyword)
.searchIn(searchIn)
.build();
let limit: number = 30;
let searchKeyword: string = "super";
let searchIn: Array<String> = ["uid", "name"];
let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.setSearchKeyword(searchKeyword)
.searchIn(searchIn)
.build();
Set Status
The status based on which the users are to be fetched. The status parameter can contain one of the below two values:
- CometChat.USER_STATUS.ONLINE - will return the list of only online users.
- CometChat.USER_STATUS.OFFLINE - will return the list of only offline users.
If this parameter is not set, will return all the available users.
- Set Status
- Typescript
let limit = 30;
let usersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.setStatus(CometChat.USER_STATUS.ONLINE)
.build()
let limit: number = 30;
let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.setStatus(CometChat.USER_STATUS.ONLINE)
.build();
Hide Blocked Users
This method is used to determine if the blocked users should be returned as a part of the user list. If set to true, the user list will not contain the users blocked by the logged in user.
- Hide Blocked Users
- Typescript
let limit = 30;
let usersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.hideBlockedUsers(true)
.build();
let limit: number = 30;
let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.hideBlockedUsers(true)
.build();
Set Roles
This method allows you to fetch the users based on multiple roles.
- Set Roles
- Typescript
let limit = 30;
let roles = ["default", "dev"];
let usersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.setRoles(roles)
.build();
let limit: number = 30;
let roles: Array<String> = ["default", "dev"];
let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.setRoles(roles)
.build();
Friends Only
This property when set to true will return only the friends of the logged-in user.
- Friends Only
- Typescript
let limit = 30;
let usersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.friendsOnly(true)
.build();
let limit: number = 30;
let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.friendsOnly(true)
.build();
Set Tags
This method accepts a list of tags based on which the list of users is to be fetched. The list fetched will only contain the users that have been tagged with the specified tags.
- Set Tags
- Typescript
let limit = 30;
let tags = ["tag1", "tag2"];
let usersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.setTags(tags)
.build();
let limit: number = 30;
let tags: Array<String> = ["tag1", "tag2"];
let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.setTags(tags)
.build();
With Tags
This property when set to true will fetch tags data along with the list of users.
- With Tags
- Typescript
let limit = 30;
let usersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.withTags(true)
.build();
let limit: number = 30;
let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.withTags(true)
.build();
Set UIDs
This method accepts a list of UIDs based on which the list of users is fetched. A maximum of 25
users can be fetched.
- Set UIDs
- Typescript
let limit = 30;
let UIDs = ["cometchat-uid-1", "cometchat-uid-2"];
let usersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.setUIDs(UIDs)
.build();
let limit: number = 30;
let UIDs: Array<String> = ["cometchat-uid-1", "cometchat-uid-2"];
let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.setUIDs(UIDs)
.build();
Sort By
This method allows you to sort the User List by a specific property of User. By default the User List is sorted by status => name => UID
. If name
is pass to the sortBy()
method the user list will be sorted by name => UID
.
- Javascript
- Typescript
let limit = 30;
let UIDs = ["cometchat-uid-1", "cometchat-uid-2"];
let sortBy = "name";
let usersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.sortBy(sortBy)
.build();
let limit: number = 30;
let sortBy: string = "name";
let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.sortBy(sortBy)
.build();
Sort By Order
This method allows you to sort the User List in a specific order. By default the user list is sorted in ascending order.
- Javascript
- Typescript
let limit = 30;
let sortByOrder = "desc";
let usersReques = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.sortByOrder(sortOrder)
.build();
let limit: number = 30;
let sortOrder: string = "desc";
let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.sortOrder(sortOrder)
.build();
Finally, once all the parameters are set to the builder class, you need to call the build() method to get the object of the UsersRequest class.
Once you have the object of the UsersRequest class, you need to call the fetchNext() method. Calling this method will return a list of User objects containing n number of users where n is the limit set in the builder class.
- Users Request
- Typescript
var limit = 30;
var usersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.build();
usersRequest.fetchNext().then(
userList => {
console.log("User list received:", userList);
}, error => {
console.log("User list fetching failed with error:", error);
}
);
let limit: number = 30;
let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.build();
usersRequest.fetchNext().then(
(userList: CometChat.User[]) => {
console.log("User list received:", userList);
}, (error: CometChat.CometChatException) => {
console.log("User list fetching failed with error:", error);
}
);
Retrieve Particular User Details
To get the information of a user, you can use the getUser()
method.
- Retrieve Particular User's Details
- Typescript
let UID = "UID";
CometChat.getUser(UID).then(
user => {
console.log("User details fetched for user:", user);
}, error => {
console.log("User details fetching failed with error:", error);
}
);
let UID: string = "UID";
CometChat.getUser(UID).then(
(user: CometChat.User) => {
console.log("User details fetched for user:", user);
}, (error: CometChat.CometChatException) => {
console.log("User details fetching failed with error:", error);
}
);
The getUser()
method takes the following parameters:
Parameter | Description |
---|---|
UID | The UID of the user for whom the details are to be fetched |
It returns the User
object containing the details of the user.
Get online user count
To get the total count of online users for your app, you can use the getOnlineUserCount()
method.
- Get Online User Count
- Typescript
CometChat.getOnlineUserCount().then(
userCount => {
console.log("Total online user count:", userCount);
}, error => {
console.log("Online user count fetching failed with error:", error);
}
);
CometChat.getOnlineUserCount().then(
(userCount: number) => {
console.log("Total online user count:", userCount);
}, (error: CometChat.CometChatException) => {
console.log("Online user count fetching failed with error:", error);
}
);
This method returns the total online user count for your app.