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:
JavaScript
TypeScript
Async/Await
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 )
);
const usersList : string [] = [ "UID1" , "UID2" , "UID3" ];
CometChat . blockUsers ( usersList ). then (
( result : Object ) => {
console . log ( "Block result:" , result );
},
( error : CometChat . CometChatException ) => {
console . log ( "Failed to block:" , error );
}
);
async function blockUser ( uid ) {
try {
const result = await CometChat . blockUsers ([ uid ]);
console . log ( "User blocked:" , result );
return result ;
} catch ( error ) {
console . error ( "Failed to block:" , error );
throw error ;
}
}
async function blockMultipleUsers ( uids ) {
try {
const result = await CometChat . blockUsers ( uids );
console . log ( "Users blocked:" , result );
return result ;
} catch ( error ) {
console . error ( "Failed to block:" , error );
throw error ;
}
}
// Usage
await blockUser ( "user123" );
await blockMultipleUsers ([ "user1" , "user2" , "user3" ]);
Returns an object with UIDs as keys and "success" or "fail" as values.
Unblock Users
Unblock previously blocked users:
JavaScript
TypeScript
Async/Await
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 )
);
const usersList : string [] = [ "UID1" , "UID2" , "UID3" ];
CometChat . unblockUsers ( usersList ). then (
( result : Object ) => {
console . log ( "Unblock result:" , result );
},
( error : CometChat . CometChatException ) => {
console . log ( "Failed to unblock:" , error );
}
);
async function unblockUser ( uid ) {
try {
const result = await CometChat . unblockUsers ([ uid ]);
console . log ( "User unblocked:" , result );
return result ;
} catch ( error ) {
console . error ( "Failed to unblock:" , error );
throw error ;
}
}
// Usage
await unblockUser ( "user123" );
Get Blocked Users List
Fetch the list of blocked users using BlockedUsersRequestBuilder:
JavaScript
TypeScript
Async/Await
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 )
);
const limit : number = 30 ;
const blockedUsersRequest : CometChat . BlockedUsersRequest =
new CometChat . BlockedUsersRequestBuilder ()
. setLimit ( limit )
. build ();
blockedUsersRequest . fetchNext (). then (
( userList : CometChat . User []) => console . log ( "Blocked users:" , userList ),
( error : CometChat . CometChatException ) => console . log ( "Failed:" , error )
);
async function fetchBlockedUsers ( limit = 30 ) {
try {
const blockedUsersRequest = new CometChat . BlockedUsersRequestBuilder ()
. setLimit ( limit )
. build ();
const userList = await blockedUsersRequest . fetchNext ();
console . log ( "Blocked users:" , userList );
return userList ;
} catch ( error ) {
console . error ( "Failed to fetch blocked users:" , error );
throw error ;
}
}
// Usage
const blockedUsers = await fetchBlockedUsers ();
Builder Options
Method Description 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 ();
Direction Description 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 )
);
async function searchBlockedUsers ( keyword ) {
try {
const blockedUsersRequest = new CometChat . BlockedUsersRequestBuilder ()
. setLimit ( 30 )
. setSearchKeyword ( keyword )
. build ();
const userList = await blockedUsersRequest . fetchNext ();
console . log ( "Search results:" , userList );
return userList ;
} catch ( error ) {
console . error ( "Failed to search blocked users:" , error );
throw error ;
}
}
// Usage
const results = await searchBlockedUsers ( "john" );
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:
Feature Behavior Messages Cannot send or receive messages Calls Cannot initiate or receive calls Typing Indicators Not sent or received Read Receipts Not sent or received Presence Online status not visible Conversations Existing 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