Skip to main content
Quick Reference for AI Agents & Developers
// Block users
await CometChat.blockUsers(["user1", "user2"]);

// Unblock users
await CometChat.unblockUsers(["user1"]);

// Fetch blocked users list
const blockedRequest = new CometChat.BlockedUsersRequestBuilder()
  .setLimit(30)
  .setDirection(CometChat.BlockedUsersRequest.directions.BLOCKED_BY_ME)
  .build();
const blockedUsers = await blockedRequest.fetchNext();

// Check block status on user object
const user = await CometChat.getUser("user_uid");
const blockedByMe = user.getBlockedByMe();
const hasBlockedMe = user.getHasBlockedMe();
Blocking users prevents all communication between the logged-in user and the blocked user. Messages, calls, and other interactions are completely blocked in both directions.
Availability: SDK, API, UI KitsBlocking is bidirectional - neither party can communicate with the other once blocked.

Block Users

Block one or more users by their UIDs:
const usersList = ["UID1", "UID2", "UID3"];

CometChat.blockUsers(usersList).then(
  (result) => {
    console.log("Block result:", result);
    // { UID1: "success", UID2: "success", UID3: "success" }
  },
  (error) => console.log("Failed to block:", error)
);
Returns an object with UIDs as keys and "success" or "fail" as values.

Unblock Users

Unblock previously blocked users:
const usersList = ["UID1", "UID2", "UID3"];

CometChat.unblockUsers(usersList).then(
  (result) => {
    console.log("Unblock result:", result);
    // { UID1: "success", UID2: "success", UID3: "success" }
  },
  (error) => console.log("Failed to unblock:", error)
);

Get Blocked Users List

Fetch the list of blocked users using BlockedUsersRequestBuilder:
const limit = 30;

const blockedUsersRequest = new CometChat.BlockedUsersRequestBuilder()
  .setLimit(limit)
  .build();

blockedUsersRequest.fetchNext().then(
  (userList) => console.log("Blocked users:", userList),
  (error) => console.log("Failed to fetch:", error)
);

Builder Options

MethodDescription
setLimit(limit)Number of users to fetch per request (max 100)
setSearchKeyword(keyword)Search blocked users by name
setDirection(direction)Filter by block direction

Filter by Direction

const blockedUsersRequest = new CometChat.BlockedUsersRequestBuilder()
  .setLimit(30)
  .setDirection(CometChat.BlockedUsersRequest.directions.BLOCKED_BY_ME)
  .build();
DirectionDescription
BLOCKED_BY_MEUsers you have blocked
HAS_BLOCKED_MEUsers who have blocked you
BOTHBoth directions (default)

Search Blocked Users

const blockedUsersRequest = new CometChat.BlockedUsersRequestBuilder()
  .setLimit(30)
  .setSearchKeyword("john")
  .build();

blockedUsersRequest.fetchNext().then(
  (userList) => console.log("Search results:", userList),
  (error) => console.log("Failed:", error)
);

Check Block Status

When you have a user object, check the block relationship:
const user = await CometChat.getUser("user1");

// Check if you blocked this user
const blockedByMe = user.getBlockedByMe();

// Check if this user blocked you
const hasBlockedMe = user.getHasBlockedMe();

if (blockedByMe) {
  console.log("You have blocked this user");
}

if (hasBlockedMe) {
  console.log("This user has blocked you");
}

Implementation Example

class BlockManager {
  constructor() {
    this.blockedUsers = new Set();
  }

  async loadBlockedUsers() {
    const request = new CometChat.BlockedUsersRequestBuilder()
      .setLimit(100)
      .setDirection(CometChat.BlockedUsersRequest.directions.BLOCKED_BY_ME)
      .build();

    try {
      const users = await request.fetchNext();
      users.forEach((user) => this.blockedUsers.add(user.getUid()));
    } catch (error) {
      console.log("Failed to load blocked users:", error);
    }
  }

  isBlocked(uid) {
    return this.blockedUsers.has(uid);
  }

  async blockUser(uid) {
    try {
      await CometChat.blockUsers([uid]);
      this.blockedUsers.add(uid);
      console.log("User blocked:", uid);
    } catch (error) {
      console.log("Failed to block:", error);
    }
  }

  async unblockUser(uid) {
    try {
      await CometChat.unblockUsers([uid]);
      this.blockedUsers.delete(uid);
      console.log("User unblocked:", uid);
    } catch (error) {
      console.log("Failed to unblock:", error);
    }
  }

  async toggleBlock(uid) {
    if (this.isBlocked(uid)) {
      await this.unblockUser(uid);
    } else {
      await this.blockUser(uid);
    }
  }
}

// Usage
const blockManager = new BlockManager();
await blockManager.loadBlockedUsers();

// Check if user is blocked
if (blockManager.isBlocked("user123")) {
  console.log("User is blocked");
}

// Toggle block status
await blockManager.toggleBlock("user123");

What Happens When Blocked

When a user is blocked:
FeatureBehavior
MessagesCannot send or receive messages
CallsCannot initiate or receive calls
Typing IndicatorsNot sent or received
Read ReceiptsNot sent or received
PresenceOnline status not visible
ConversationsExisting conversations remain but are inactive
Blocking is immediate. Any pending messages or calls will fail once the block is in place.

Best Practices

  • Show clear feedback when blocking/unblocking
  • Provide confirmation before blocking
  • Allow users to view and manage their block list
  • Hide blocked users from search results and user lists
  • Handle cases where block/unblock fails
  • Refresh block list periodically
  • Cache block status locally for quick checks

Next Steps