Quick Reference - Block and unblock users:
Block Users
In other words, as a logged-in user, how do I block a user from sending me messages? You can block users using theblockUsers() 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. The blockUsers() method takes a Array as a parameter which holds the list of UID's to be blocked.
- JavaScript
- TypeScript
Response
Response
On Success —
Result Object (per UID):
blockUsers() returns an object with each UID as key and result object as value:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
[UID] | object | Result object for each blocked UID | See below |
| Parameter | Type | Description | Sample Value |
|---|---|---|---|
success | boolean | Whether the block operation succeeded | true |
message | string | Descriptive message about the operation | "The user with UID cometchat-uid-7 has blocked user with UID cometchat-uid-2 successfully." |
UID's as the keys and “success” or “fail” as the value based on if the block operation for the UID was successful or not.
Unblock Users
In other words, as a logged-in user, how do I unblock a user I previously blocked? You can unblock the already blocked users using theunblockUsers() method. You can unblock multiple users in a single operation. The unblockUsers() method takes a Array as a parameter which holds the list of UID's to be unblocked.
- JavaScript
- TypeScript
Response
Response
On Success —
Result Object (per UID):
unblockUsers() returns an object with each UID as key and result object as value:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
[UID] | object | Result object for each unblocked UID | See below |
| Parameter | Type | Description | Sample Value |
|---|---|---|---|
success | boolean | Whether the unblock operation succeeded | true |
message | string | Descriptive message about the operation | "The user with UID cometchat-uid-7 has unblocked user with UID cometchat-uid-2 successfully." |
UID's as the keys and success or fail as the value based on if the unblock operation for the UID was successful or not.
Get List of Blocked Users
In other words, as a logged-in user, how do I get a list of all users I’ve blocked? In order to fetch the list of blocked users, you can use theBlockedUsersRequest 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:
Set Limit
This method sets the limit i.e. the number of blocked users that should be fetched in a single iteration.- JavaScript
- TypeScript
Set Search Keyword
This method allows you to set the search string based on which the blocked users are to be fetched.- JavaScript
- TypeScript
Response
Response
On Success —
fetchNext() with search filter returns an array of blocked User objects matching the search:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "cometchat-uid-2" |
name | string | Display name of the user | "George Alan" |
avatar | string | URL to user’s avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp" |
status | string | User’s online status | "offline" |
role | string | User’s role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1772104172 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | true |
deactivatedAt | number | Timestamp when user was deactivated (0 if active) | 0 |
blockedByMeAt | number | Timestamp when blocked by current user | 1772173462 |
blockedAt | number | Timestamp of block action | 1772173462 |
conversationId | string | Conversation ID between this user and logged-in user | "cometchat-uid-2_user_cometchat-uid-6" |
Set Direction
- 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.
- CometChat.BlockedUsersRequest.directions.HAS_BLOCKED_ME - This will ensure that the list of blocked users only contains the users that have blocked the logged in user.
- 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.
- JavaScript
- TypeScript
Response (BLOCKED_BY_ME)
Response (BLOCKED_BY_ME)
On Success —
fetchNext() with BLOCKED_BY_ME direction returns users blocked by the logged-in user:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "cometchat-uid-2" |
name | string | Display name of the user | "George Alan" |
avatar | string | URL to user’s avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp" |
status | string | User’s online status | "offline" |
role | string | User’s role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1772104172 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | true |
deactivatedAt | number | Timestamp when user was deactivated (0 if active) | 0 |
blockedByMeAt | number | Timestamp when blocked by current user | 1772173462 |
blockedAt | number | Timestamp of block action | 1772173462 |
conversationId | string | Conversation ID between this user and logged-in user | "cometchat-uid-2_user_cometchat-uid-6" |
Response (HAS_BLOCKED_ME)
Response (HAS_BLOCKED_ME)
On Success —
fetchNext() with HAS_BLOCKED_ME direction returns users who have blocked the logged-in user. Returns an empty array if no users have blocked you.Response (BOTH)
Response (BOTH)
On Success —
fetchNext() with BOTH direction returns all blocked users (both directions):| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "cometchat-uid-2" |
name | string | Display name of the user | "George Alan" |
avatar | string | URL to user’s avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp" |
status | string | User’s online status | "offline" |
role | string | User’s role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1772104172 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | true |
deactivatedAt | number | Timestamp when user was deactivated (0 if active) | 0 |
blockedByMeAt | number | Timestamp when blocked by current user | 1772173462 |
blockedAt | number | Timestamp of block action | 1772173462 |
conversationId | string | Conversation ID between this user and logged-in user | "cometchat-uid-2_user_cometchat-uid-6" |
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.
- JavaScript
- TypeScript
Response
Response
On Success —
fetchNext() returns an array of blocked User objects:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "123456" |
name | string | Display name of the user | "Farhan Ahmed" |
avatar | string | URL to user’s avatar image | "https://st2.depositphotos.com/38197074/46684/v/450/depositphotos_466848082-stock-illustration-initial-letter-vector-logo.jpg" |
status | string | User’s online status | "offline" |
role | string | User’s role | "extrarole" |
lastActiveAt | number | Unix timestamp of last activity | 1768988601 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | true |
deactivatedAt | number | Timestamp when user was deactivated (0 if active) | 0 |
metadata | object | Custom metadata attached to the user | {"meta": "anyValue"} |
blockedByMeAt | number | Timestamp when blocked by current user | 1772164515 |
blockedAt | number | Timestamp of block action | 1772164515 |
conversationId | string | Conversation ID between this user and logged-in user | "123456_user_cometchat-uid-7" |
Best Practices
Use hideBlockedUsers when fetching user lists
Use hideBlockedUsers when fetching user lists
When displaying user lists in your app, use
hideBlockedUsers(true) in the UsersRequestBuilder to automatically exclude blocked users from the results.Handle block/unblock results per UID
Handle block/unblock results per UID
The
blockUsers() and unblockUsers() methods return a map with each UID’s result (“success” or “fail”). Check individual results rather than assuming all operations succeeded.Troubleshooting
Blocked user can still send messages
Blocked user can still send messages
Blocking is enforced server-side. If a blocked user’s messages still appear, verify the block operation returned “success” for that UID. Also ensure you’re not using a cached conversation list — refresh after blocking.
fetchNext returns empty for blocked users list
fetchNext returns empty for blocked users list
Check the
setDirection filter. If set to BLOCKED_BY_ME, only users you blocked are returned. If set to HAS_BLOCKED_ME, only users who blocked you are returned. Use BOTH to see all.