CometChat Pulse Documentation

Welcome to the CometChat Pulse documentation. You'll find comprehensive guides and documentation to help you start working with CometChat as quickly as possible. Let's jump right in!

Get Logged In User Details

You can get the details of the logged in user using the getLoggedInUser() method.

var user = CometChat.getLoggedinUser().then(user=>{
  console.log("user details:", {user});
},error=>{
  console.log("error getting details:", {error})
});

This method will return a User object containing all the information related to the logged in user.

Get 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:

  1. setLimit(limit:number) - This method sets the limit i.e. the number of users that should be fetched in a single iteration.
let usersRequest = new CometChat.UsersRequestBuilder().setLimit(30).build();
  1. setSearchKeyWord(searchString : string ) - This method allows you to set the search string based on which the users are to be fetched.
let usersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.setSearchKeyWord("abc")
.build();
  1. setStatus(status : string) - The status based on which the users are to be fetched. The status parameter can contain one of the below two values:
    a. CometChat.USER_STATUS.ONLINE - will return the list of only online users.
    b. CometChat.USER_STATUS.OFFLINE - will return the list of only offline users.
    If this parameter is not set, will return all the available users.
let usersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.setUserStatus(CometChat.USER_STATUS.ONLINE)
.build();
  1. hideBlockedUsers(hideBlockedUsers:boolean) - 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.
let usersRequest = new CometChat.UsersRequestBuilder()
.setLimit(limit)
.hideBlockedUsers(true)
.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.

var limit = 30;
var usersRequest = new CometChat.UsersRequestBuilder().setLimit(limit).build();

usersRequest.fetchNext().then(
  userList => {
    /* userList will be the list of User class. */
    console.log("User list received:", userList);
    /* retrived list can be used to display contact list. */
  },
  error => {
    console.log("User list fetching failed with error:", error);
  }
);

Want to show only friends?

By default, the above method returns all users. To fetch only friends, you can enable the Show only friends setting in the CometChat Dashboard. You can add friends for a particular user using our APIs.

Get User Information

To get the information of a user, you can use the getUser() method.

var 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);
 }
);

The getUser() method takes the following parameters:

Parameter
Description

UID

The UID of the user for whom the details are to be fetched

On success, the CometChat.User object containing the details of the user is returned.

Receive User Events

Wherever you wish to receive user events in, you need to register the UserListener using the addUserListener() method.

var listenerID = "UNIQUE_LISTENER_ID";

CometChat.addUserListener(
 listenerID,
 new CometChat.UserListener({
  onUserOnline: onlineUser => {
   /* when someuser/friend comes online, user will be received here */
   console.log("On User Online:", { onlineUser });
  },
  onUserOffline: offlineUser => {
   /* when someuser/friend went offline, user will be received here */
   console.log("On User Offline:", { offlineUser });
  }
 })
);
Parameter
Description

listenerID

An ID that uniquely identifies that listener. We recommend using the activity or fragment name.

You will receive an object of the CometChat.User class on Presenceresolved

We recommend you remove the listener once it is not in use.

listenerID = "UNIQUE_LISTENER_ID";
CometChat.removeUserListener(listenerID);

Block Users

You can block users using the blockUsers() method. Once any user is blocked, all the communication to and from the respective user will be completely blocked. You can block multiple users in a single operation. TheblockUsers() method takes an array[string] as a parameter which holds the list of UIDs to be blocked.

var usersList = ["UID1", "UID2", "UID3"];
CometChat.blockUsers(usersList).then(list => {
    console.log("users list blocked", { list });
}, error => {
    console.log("Blocking user fails with error", error);
});

on promise resolved, you receive an array which contains UIDs as the keys and "success" or "fail" as the value based on if the block operation for the UID was successful or not.

Unblock User

You can unblock the already blocked users using the unblockUsers() method. You can unblock multiple users in a single operation. You must pass list of UID/s to be unblocked as a parameter to unblockUsers().

Example : Let us assume, we have blocked user "UID1", "UID2", "UID3" and we would like to unblock these users.


var usersList = ["UID1", "UID2", "UID3"];


CometChat.unblockUsers(usersList).then(list => {
    console.log("users list unblocked", { list });
}, error => {
    console.log("unblocking user fails with error", error);
});

on promise resolved, you receive an array which contains UIDs as the keys and "success" or "fail" as the value based on if the unblock operation for the UID was successful or not.

Get the list of blocked users.

In order to fetch the list of blocked users, you can use the BlockedUsersRequest class. To use this class i.e to create an object of the BlockedUsersRequest class, you need to use the BlockedUsersRequestBuilder class. The BlockedUsersRequestBuilder class allows you to set the parameters based on which the blocked users are to be fetched.

The BlockedUsersRequestBuilder class allows you to set the below parameters:

  1. setLimit(limit) - This method sets the limit i.e. the number of blocked users that should be fetched in a single iteration.

  2. setSearchKeyWord(searchString) - This method allows you to set the search string based on which the blocked users are to be fetched.

  3. setDirection(direction) - This can hold one of the below values:
    a.CometChat.BlockedUsersRequest.directions.BLOCKED_BY_ME - This will ensure that the list of blocked users only contains the users blocked by the logged in user.
    b. CometChat.BlockedUsersRequest.directions.HAS_BLOCKED_ME - setting the direction to this will return only the list of users that have blocked the logged in user.
    c. CometChat.BlockedUsersRequest.directions.BOTH - this will make sure the list of users includes both the above cases. This is the default value for the direction variable if it is not set.

Finally, once all the parameters are set to the builder class, you need to call the build() method to get the object of the BlockedUsersRequest class.

Once you have the object of the BlockedUsersRequest class, you need to call the fetchNext() method. Calling this method will return a list of User objects containing n number of blocked users where n is the limit set in the builder class.

var limit = 30;
var blockedUsersRequest = new CometChat.BlockedUsersRequestBuilder().setLimit(limit).build();
blockedUsersRequest.fetchNext().then(
    userList => {        
        console.log("Blocked user list received:", userList);        
    },
    error => {
        console.log("Blocked user list fetching failed with error:", error);
    }
); 

What's Next

Groups

Users


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.