Skip to main content
FieldValue
Package@cometchat/chat-uikit-angular
Key methodsCometChat.blockUsers(), CometChat.unblockUsers()
EventsCometChatUserEvents.ccUserBlocked, CometChatUserEvents.ccUserUnblocked
UI helperscometchat-confirm-dialog
InitCometChatUIKit.init(uiKitSettings) then CometChatUIKit.login("UID")
Sample appGitHub
RelatedAll Guides
Block/Unblock lets users prevent specific users from sending them messages. When blocked, the message composer is hidden and replaced with an unblock prompt. Before starting, complete the Integration Guide.

Components

Component / MethodRole
CometChat.blockUsers()SDK method to block specific users
CometChat.unblockUsers()SDK method to unblock previously blocked users
CometChatUserEvents.ccUserBlockedRxJS subject fired when a user is blocked
CometChatUserEvents.ccUserUnblockedRxJS subject fired when a user is unblocked
cometchat-confirm-dialogConfirmation dialog for block/unblock actions

Implementation Steps

1. Block User

Call CometChat.blockUsers() with the target UID. On success, update the local user object with setBlockedByMe(true) and emit ccUserBlocked so all subscribed components react to the change.
import { CometChat } from '@cometchat/chat-sdk-javascript';
import {
  CometChatUserEvents,
  CometChatLocalize
} from '@cometchat/chat-uikit-angular';

async blockUser(user: CometChat.User): Promise<void> {
  const uid = user.getUid();

  try {
    await CometChat.blockUsers([uid]);
    user.setBlockedByMe(true);
    CometChatUserEvents.ccUserBlocked.next(user);
    this.toastMessage = CometChatLocalize.getLocalizedString('blocked_successfully');
    this.showToast = true;
  } catch (error) {
    console.error('Blocking user failed:', error);
  }
}

2. Unblock User

Call CometChat.unblockUsers() with the target UID. On success, update the local user object and emit ccUserUnblocked to restore the composer.
unblockUser(user: CometChat.User): void {
  const uid = user.getUid();

  CometChat.unblockUsers([uid])
    .then(() => {
      user.setBlockedByMe(false);
      CometChatUserEvents.ccUserUnblocked.next(user);
    })
    .catch((error) => {
      console.error('Unblocking user failed:', error);
    });
}

3. Confirmation Dialog

Show a confirmation dialog before blocking. This prevents accidental blocks.
@if (showBlockUserDialog) {
  <div class="cometchat-block-user-dialog__backdrop">
    <cometchat-confirm-dialog
      [title]="'block_contact' | translate"
      [messageText]="'confirm_block_contact' | translate"
      [confirmButtonText]="'user_details_block' | translate"
      (cancelClick)="showBlockUserDialog = false"
      (confirmClick)="onBlockConfirmed()">
    </cometchat-confirm-dialog>
  </div>
}
showBlockUserDialog = false;

promptBlockUser(): void {
  this.showBlockUserDialog = true;
}

async onBlockConfirmed(): Promise<void> {
  this.showBlockUserDialog = false;
  await this.blockUser(this.selectedUser!);
}

4. Composer Blocked State

When a user is blocked, the composer is replaced with an unblock prompt.
@if (showComposer) {
  <cometchat-message-composer
    [user]="selectedUser">
  </cometchat-message-composer>
} @else {
  <div class="message-composer-blocked" (click)="unblockUser(selectedUser!)">
    <div class="message-composer-blocked__text">
      {{ 'cannot_send_to_blocked_user' | translate }}
      <a>{{ 'click_to_unblock' | translate }}</a>
    </div>
  </div>
}

5. Event Listeners

Subscribe to block/unblock events to update the UI in real time. Clean up subscriptions in ngOnDestroy.
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs';
import { CometChatUserEvents } from '@cometchat/chat-uikit-angular';

@Component({
  selector: 'app-chat-messages',
  standalone: true,
  imports: [],
  template: `<!-- see steps above -->`
})
export class ChatMessagesComponent implements OnInit, OnDestroy {
  showComposer = true;
  private subscriptions: Subscription[] = [];

  ngOnInit(): void {
    this.subscriptions.push(
      CometChatUserEvents.ccUserBlocked.subscribe((user) => {
        if (user.getBlockedByMe()) {
          this.showComposer = false;
        }
      })
    );

    this.subscriptions.push(
      CometChatUserEvents.ccUserUnblocked.subscribe((user) => {
        if (!user.getBlockedByMe()) {
          this.showComposer = true;
        }
      })
    );
  }

  ngOnDestroy(): void {
    this.subscriptions.forEach(sub => sub.unsubscribe());
  }
}

Feature Matrix

FeatureComponent / MethodDescription
Block userCometChat.blockUsers([uid])Blocks a user by UID
Unblock userCometChat.unblockUsers([uid])Unblocks a previously blocked user
Check blocked statususer.getBlockedByMe()Returns whether the user is blocked
Block confirmationcometchat-confirm-dialogPrevents accidental blocks
Blocked composer stateshowComposer flagHides composer and shows unblock prompt
Block eventCometChatUserEvents.ccUserBlockedRxJS subject for block notifications
Unblock eventCometChatUserEvents.ccUserUnblockedRxJS subject for unblock notifications
Subscription cleanupngOnDestroyUnsubscribes from all event listeners

Next Steps

Users

Display and manage user lists.

Message Composer

Customize the message input component.

All Guides

Browse all feature and formatter guides.

Sample App

Full working sample application on GitHub.