# Contacts (/en/realtime-media/im/build/build-core-messaging/contacts/react-native)

> For AI agents: see the complete documentation index at [llms.txt](/llms.txt).

After logging in to Chat, users can start adding contacts and chatting with each other. They can also manage these contacts, for example, by adding, retrieving and removing contacts. They can also add the specified user to the blocklist to stop receiving messages from that user.

    Agora Chat, by default, allows two users to send chat messages to each other as strangers. This means that users can chat without adding each other as a contact. If you only allow chat between contacts, you can contact [support@agora.io](mailto\:support@agora.io) to enable the friend relationship check switch. After this function is enabled, the SDK will check whether the two users trying to chat are on the contact list of each other. If no, the SDK will report error code 221.

    Agora Chat, by default, allows two users to send chat messages to each other as strangers. This means that users can chat without adding each other as a contact. If you only allow chat between contacts, you can contact [support@agora.io](mailto\:support@agora.io) to enable the friend relationship check switch. After this function is enabled, the SDK will check whether the two users trying to chat are on the contact list of each other. If no, the SDK will report error code 221.

    This page shows how to use the Chat SDK to implement contact management.

    ## Understand the tech [#understand-the-tech-3]

    The Chat SDK uses `ChatContactManager` to add, remove and manage contacts. The following are the core methods:

    * `addContact`: Adds a contact.
    * `acceptInvitation`: Accepts the contact invitation.
    * `declineInvitation`: Declines the contact invitation.
    * `deleteContact`: Deletes a contact.
    * `getAllContactsFromServer`: Retrieves a list of contacts from the server.
    * `getAllContactsFromDB`: Retrieves all contacts from the local database.
    * `addUserToBlockList`: Adds the specified user to the blocklist.
    * `removeUserFromBlockList`: Removes the specified user from the blocklist.
    * `getBlockListFromServer`: Retrieves a list of blocked users from the server.

    ## Prerequisites [#prerequisites-3]

    Before proceeding, ensure that you meet the following requirements:

    * You have integrated the Chat SDK, initialized the SDK and implemented the functionality of registering accounts and login. For details, see [Chat SDK quickstart](../../get-started-sdk).
    * You understand the API call frequency limits as described in [Limitations](../limitations).

    ## Implementation [#implementation-3]

    This section shows how to manage contacts with the methods provided by the Chat SDK.

    ### Add a contact [#add-a-contact-1]

    This section uses user A and user B as an example to describe the process of adding a peer user as a contact.

    User A call `addContact` to add user B as a contact:

    ```typescript
    // Specify the user ID.
    const userId = "foo";
    // Set the reason for adding a contact.
    const reason = "Request to add a friend.";
    ChatClient.getInstance()
      .contactManager.addContact(userId, reason)
      .then(() => {
        console.log("request send success.");
      })
      .catch((reason) => {
        console.log("request send fail.", reason);
      });
    ```

    When user B receives the contact invitation, accept or decline the invitation.

    * To accept the invitation

      ```typescript
      const userId = "bar";
      ChatClient.getInstance()
        .contactManager.acceptInvitation(userId)
        .then(() => {
          console.log("accept request success.");
        })
        .catch((reason) => {
          console.log("accept request fail.", reason);
        });
      ```

    * To decline the invitation

      ```typescript
      const userId = "bar";
      ChatClient.getInstance()
        .contactManager.declineInvitation(userId)
        .then(() => {
          console.log("decline request success.");
        })
        .catch((reason) => {
          console.log("decline request fail.", reason);
        });
      ```

    User A uses `ContactEventListener` to listen for contact events.

    * If user B accepts the invitation, `onContactInvited` is triggered.

      ```typescript
      const contactEventListener = new (class implements ChatContactEventListener {
        that: any;
        constructor(parent: any) {
          this.that = parent;
        }
        onContactInvited(userId: string, reason?: string): void {
          console.log(`onContactInvited: ${userId}, ${reason}: `);
        }
      })(this);
      ChatClient.getInstance().contactManager.addContactListener(
        contactEventListener
      );
      ```

    * If user B declines the invitation, `onFriendRequestDeclined` is triggered.

      ```typescript
      const contactEventListener = new (class implements ChatContactEventListener {
        that: any;
        constructor(parent: any) {
          this.that = parent;
        }
        onFriendRequestDeclined(userId: string): void {
          console.log(`onFriendRequestDeclined: ${userId}: `);
        }
      })(this);
      ChatClient.getInstance().contactManager.addContactListener(
        contactEventListener
      );
      ```

    #### Delete a contact [#delete-a-contact-3]

    Call `deleteContact` to delete the specified contact. To prevent mis-operation, we recommend adding a double check process before deleting the contact.

    ```typescript
    // Specify the user to be deleted.
    const userId = "tom";
    // Whether to delete the conversation.
    const keepConversation = true;
    ChatClient.getInstance()
      .contactManager.deleteContact(userId, keepConversation)
      .then(() => {
        console.log("remove success.");
      })
      .catch((reason) => {
        console.log("remove fail.", reason);
      });
    ```

    ### Add a user to the blocklist [#add-a-user-to-the-blocklist-2]

    Call `addUserToBlockList` to add the specified user to the blocklist.

    You can add any other users to the blocklist, regardless of whether they are on the contact list or not. Contacts are still displayed on the contact list even if they are added to the blocklist. After adding users to the blocklist, you can still send messages to them, but will not receive messages from them as they cannot send messages or friend requests to you.

    ```typescript
    // Specify the user ID to be added to the blocklist.
    const userId = "tom";
    // Call addUserToBlockList.
    ChatClient.getInstance()
      .contactManager.addUserToBlockList(userId)
      .then(() => {
        console.log("add blocklist success.");
      })
      .catch((reason) => {
        console.log("add blocklist fail.", reason);
      });
    ```

    ### Retrieve the blocklist [#retrieve-the-blocklist-1]

    To get the blocklist from the local device, call `getBlockListFromDB`.

    ```typescript
    ChatClient.getInstance()
      .contactManager.getBlockListFromDB()
      .then((list) => {
        console.log("get blocklist success: ", list);
      })
      .catch((reason) => {
        console.log("get blocklist fail.", reason);
      });
    ```

    You can also retrieve the blocklist from the server by calling `getBlockListFromServer`.

    ```typescript
    ChatClient.getInstance()
      .contactManager.getBlockListFromServer()
      .then((list) => {
        console.log("get blocklist success: ", list);
      })
      .catch((reason) => {
        console.log("get blocklist fail.", reason);
      });
    ```

    ### Remove a user from the blocklist [#remove-a-user-from-the-blocklist-2]

    To remove the specified user from the blocklist, call `removeUserFromBlockList`.

    ```typescript
    const userId = "tom";
    // Call removeUserFromBlockList
    ChatClient.getInstance()
      .contactManager.removeUserFromBlockList(userId)
      .then((list) => {
        console.log("remove user to blocklist success: ", list);
      })
      .catch((reason) => {
        console.log("remove user to blocklist fail.", reason);
      });
    ```

    
  
      
  
      
  
      
  
