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

# Block Users

> Block and unblock users in chat apps with the CometChat JavaScript SDK.

<Accordion title="AI Integration Quick Reference">
  | Field           | Value                                                                                                                                                                                                                          |
  | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
  | Package         | `@cometchat/chat-sdk-javascript`                                                                                                                                                                                               |
  | Import          | `import { CometChat } from "@cometchat/chat-sdk-javascript";`                                                                                                                                                                  |
  | Key methods     | `CometChat.blockUsers(usersList)` · `CometChat.unblockUsers(usersList)` · `blockedUsersRequest.fetchNext()` (build via `BlockedUsersRequestBuilder`)                                                                           |
  | Request builder | `CometChat.BlockedUsersRequestBuilder` — configure with `.setLimit()` · `.setSearchKeyword()` · `.setDirection()`; then `.build()` → `.fetchNext()`                                                                            |
  | Key classes     | `CometChat.User` (access via `getBlockedByMe()`, `getHasBlockedMe()`); direction via `CometChat.BlockedUsersRequest.directions.BLOCKED_BY_ME` / `.HAS_BLOCKED_ME` / `.BOTH`                                                    |
  | Primary output  | `blockUsers()` / `unblockUsers()` resolve to a `{ uid: "success" \| "fail" }` object (each UID processed independently); `fetchNext()` resolves to `Promise<CometChat.User[]>`; all reject with `CometChat.CometChatException` |
  | Prerequisites   | SDK initialized via [`CometChat.init()`](/sdk/javascript/setup-sdk) and a logged-in user via [`CometChat.login()`](/sdk/javascript/authentication-overview)                                                                    |
  | Constraints     | `setDirection()` default is `BOTH`; `blockUsers()` / `unblockUsers()` take an array of UIDs                                                                                                                                    |
  | Related         | [Retrieve Users](/sdk/javascript/retrieve-users) · [User Presence](/sdk/javascript/user-presence) · [User Management](/sdk/javascript/user-management) · [Flag a Message](/sdk/javascript/flag-message)                        |
  | Full reference  | [`User`](/sdk/reference/entities#user)                                                                                                                                                                                         |
</Accordion>

## Block Users

Block users to prevent all communication with them. Use `blockUsers()` with an array of UIDs.

<Tabs>
  <Tab title="TypeScript">
    ```typescript theme={null}
    const 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>

  <Tab title="JavaScript">
    ```javascript theme={null}
    const usersList = ["UID1", "UID2", "UID3"];

    CometChat.blockUsers(usersList).then(
    list => {
      console.log("users list blocked", { list });
    }, error => {
      console.log("Blocking user fails with error", error);
    }
    );
    ```

    Alternatively, you can use the `async/await` syntax:

    ```javascript theme={null}
    const usersList = ["UID1", "UID2", "UID3"];

    try {
      const list = await CometChat.blockUsers(usersList);
      console.log("users list blocked", { list });
    } catch (error) {
      console.log("Blocking user fails with error", error);
    }
    ```
  </Tab>
</Tabs>

Returns an object with UIDs as keys and `"success"` or `"fail"` as values. Each [`User`](/sdk/reference/entities#user) in the request is processed independently.

## Unblock Users

Unblock previously blocked users using `unblockUsers()` with an array of UIDs.

<Tabs>
  <Tab title="TypeScript">
    ```typescript theme={null}
    const 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>

  <Tab title="JavaScript">
    ```javascript theme={null}
    const usersList = ["UID1", "UID2", "UID3"];

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

    Alternatively, you can use the `async/await` syntax:

    ```javascript theme={null}
    const usersList = ["UID1", "UID2", "UID3"];

    try {
      const list = await CometChat.unblockUsers(usersList);
      console.log("users list unblocked", { list });
    } catch (error) {
      console.log("unblocking user fails with error", error);
    }
    ```
  </Tab>
</Tabs>

Returns an object with UIDs as keys and `"success"` or `"fail"` as values. Each [`User`](/sdk/reference/entities#user) in the request is processed independently.

## Get List of Blocked Users

Use `BlockedUsersRequestBuilder` to fetch blocked users with filtering and pagination.

### Set Limit

Sets the number of blocked users to fetch per request.

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

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

### Set Search Keyword

Filters blocked users by a search string.

<Tabs>
  <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>

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

### Set Direction

Filters by block direction:

* `BLOCKED_BY_ME` — Users blocked by the logged-in user
* `HAS_BLOCKED_ME` — Users who have blocked the logged-in user
* `BOTH` — Both directions (default)

<Tabs>
  <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>

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

    </Tabs>

    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`](/sdk/reference/entities#user) objects containing n number of blocked users where N is the limit set in the builder class.

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

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

    </Tabs>

    After configuring the builder, call `build()` to get the `BlockedUsersRequest` object, then call `fetchNext()` to retrieve blocked users.

    <Tabs>
    <Tab title="TypeScript">
    ```typescript
    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>

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

The `fetchNext()` method returns an array of [`User`](/sdk/reference/entities#user) objects representing blocked users.

Relevant fields to access on returned users:

| Field        | Getter              | Return Type | Description                                      |
| ------------ | ------------------- | ----------- | ------------------------------------------------ |
| blockedByMe  | `getBlockedByMe()`  | `boolean`   | Whether the logged-in user has blocked this user |
| hasBlockedMe | `getHasBlockedMe()` | `boolean`   | Whether this user has blocked the logged-in user |

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Retrieve Users" icon="users" href="/sdk/javascript/retrieve-users">
    Fetch and filter user lists
  </Card>

  <Card title="User Presence" icon="circle-dot" href="/sdk/javascript/user-presence">
    Track online/offline status of users
  </Card>

  <Card title="User Management" icon="users-gear" href="/sdk/javascript/user-management">
    Create, update, and delete users
  </Card>

  <Card title="Flag Message" icon="flag" href="/sdk/javascript/flag-message">
    Report inappropriate messages from users
  </Card>
</CardGroup>
