> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-v6-beta2-flutter-uikit.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Block Users

> Block and unblock users, and retrieve blocked user lists using the CometChat React Native SDK.

<Info>
  **Quick Reference** - Block and unblock users:

  ```javascript theme={null}
  // Block users
  await CometChat.blockUsers(["UID1", "UID2"]);

  // Unblock users
  await CometChat.unblockUsers(["UID1", "UID2"]);

  // Fetch blocked users
  const request = new CometChat.BlockedUsersRequestBuilder().setLimit(30).build();
  const blockedUsers = await request.fetchNext();
  ```
</Info>

<Note>
  **Available via:** [SDK](/sdk/react-native/block-users) | [REST API](/rest-api/blocked-users/block-user)
</Note>

## 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 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. The `blockUsers()` method takes a `Array` as a parameter which holds the list of `UID's` to be blocked.

<Tabs>
  <Tab title="JavaScript">
    ```javascript theme={null}
    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);
    }
    );
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    var usersList: String[] = ["UID1", "UID2", "UID3"];

    CometChat.blockUsers(usersList).then(
      (list: Object) => {
          console.log("users list blocked", { list });
      }, (error: CometChat.CometChatException) => {
          console.log("Blocking user fails with error", error);
      }
    );
    ```
  </Tab>
</Tabs>

<Accordion title="Response">
  **On Success** — `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    |

  **Result Object (per UID):**

  | 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."` |
</Accordion>

It returns a Array which contains `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 the `unblockUsers()` 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.

<Tabs>
  <Tab title="JavaScript">
    ```javascript theme={null}
    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);
    }
    );
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    var usersList: String[] = ["UID1", "UID2", "UID3"];

    CometChat.unblockUsers(usersList).then(
      (list: Object) => {
          console.log("users list blocked", { list });
      }, (error: CometChat.CometChatException) => {
          console.log("Blocking user fails with error", error);
      }
    );   
    ```
  </Tab>
</Tabs>

<Accordion title="Response">
  **On Success** — `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    |

  **Result Object (per UID):**

  | 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."` |
</Accordion>

It returns a Array which contains `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 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:

### Set Limit

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

<Tabs>
  <Tab title="JavaScript">
    ```javascript theme={null}
    let limit = 30;
    let blockedUsersRequest = new BlockedUsersRequest.BlockedUsersRequestBuilder()
                      				.setLimit(limit)
                      				.build();
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let limit: number = 30;
    let blockedUsersRequest: CometChat.BlockedUsersRequest = new CometChat.BlockedUsersRequestBuilder()
      .setLimit(limit)
      .build();
    ```
  </Tab>
</Tabs>

### Set Search Keyword

This method allows you to set the search string based on which the blocked users are to be fetched.

<Tabs>
  <Tab title="JavaScript">
    ```javascript theme={null}
    let limit = 30;
    let searchKeyword = "super";
    let blockedUsersRequest = new BlockedUsersRequest.BlockedUsersRequestBuilder()
                      				.setLimit(limit)
                      				.setSearchKeyword(searchKeyword)
                      				.build();
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let limit: number = 30;
    let searchKeyword: string = "super";
    let blockedUsersRequest: CometChat.BlockedUsersRequest = new CometChat.BlockedUsersRequestBuilder()
      .setLimit(limit)
      .setSearchKeyword(searchKeyword)
      .build();
    ```
  </Tab>
</Tabs>

<Accordion title="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"`                                |
</Accordion>

### 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.

<Tabs>
  <Tab title="JavaScript">
    ```javascript theme={null}
    let limit = 30;
    let blockedUsersRequest = new BlockedUsersRequest.BlockedUsersRequestBuilder()
                      				.setLimit(limit)
                      				.setDirection(CometChat.BlockedUsersRequest.directions.BLOCKED_BY_ME)
                      				.build();
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let limit: number = 30;
    let blockedUsersRequest: CometChat.BlockedUsersRequest = new CometChat.BlockedUsersRequestBuilder()
      .setLimit(limit)
      .setDirection(CometChat.BlockedUsersRequest.directions.BLOCKED_BY_ME)
      .build();
    ```
  </Tab>
</Tabs>

<Accordion title="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"`                                |
</Accordion>

<Accordion title="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.
</Accordion>

<Accordion title="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"`                                |
</Accordion>

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.

<Tabs>
  <Tab title="JavaScript">
    ```javascript theme={null}
    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);
    }
    );
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let limit: number = 30;
    let blockedUsersRequest: CometChat.BlockedUsersRequest = new CometChat.BlockedUsersRequestBuilder()
      .setLimit(limit)
      .build();

    blockedUsersRequest.fetchNext().then(
      (userList: CometChat.User[]) => {
          console.log("Blocked user list received:", userList);
      }, (error: CometChat.CometChatException) => {
          console.log("Blocked user list fetching failed with error:", error);
      }
    );     
    ```
  </Tab>
</Tabs>

<Accordion title="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"`                                                                                                  |
</Accordion>

## Best Practices

<AccordionGroup>
  <Accordion title="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.
  </Accordion>

  <Accordion title="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.
  </Accordion>
</AccordionGroup>

## Troubleshooting

<AccordionGroup>
  <Accordion title="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.
  </Accordion>

  <Accordion title="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.
  </Accordion>
</AccordionGroup>

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Retrieve Users" icon="users" href="/sdk/react-native/retrieve-users">
    Fetch user lists with filtering and pagination
  </Card>

  <Card title="User Management" icon="user-plus" href="/sdk/react-native/user-management">
    Create, update, and delete users in CometChat
  </Card>

  <Card title="User Presence" icon="circle-dot" href="/sdk/react-native/user-presence">
    Track online/offline status of users in real time
  </Card>

  <Card title="Send Messages" icon="paper-plane" href="/sdk/react-native/send-message">
    Send text, media, and custom messages to users and groups
  </Card>
</CardGroup>
