> ## Documentation Index
> Fetch the complete documentation index at: https://docs.quickblox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Contact List

> Learn how to create, update, manage, and add users to your contact list.

The Contact List API is rather straightforward. A user A sends a request to become "friends" with a user B. The user B accepts the friend request. And now the user A and B appear in each other roster.
This feature is also useful to get online/offline statuses of the users from the user's Contact List.

Visit [Key Concepts](/docs/key-concepts) page to learn the most important QuickBlox concepts.

## Before you begin

1. Register a [QuickBlox account](https://admin.quickblox.com/signin). This is a matter of a few minutes and you will be able to use this account to build your apps.
2. Configure QuickBlox SDK for your app. Check out [Setup](/sdks/android-setup) page for more details.
3. Create a user session to be able to use QuickBlox functionality. See [Authentication](/sdks/android-authentication) page to learn how to do it.
4. Connect to the Chat server. See [Connection](/sdks/android-chat-connection) page to learn how to do it.

## Add listener

To use a contact list, you have to obtain it and set all needed listeners.

<Tabs>
  <Tab title="Java">
    ```Java theme={null}
    QBRosterListener rosterListener = new QBRosterListener() {
        @Override
        public void entriesDeleted(Collection<Integer> userIds) {

        }

        @Override
        public void entriesAdded(Collection<Integer> userIds) {

        }

        @Override
        public void entriesUpdated(Collection<Integer> userIds) {

        }

        @Override
        public void presenceChanged(QBPresence presence) {

        }
    };

    QBSubscriptionListener subscriptionListener = new QBSubscriptionListener() {
        @Override
        public void subscriptionRequested(int userId) {
        // subscription was requested by user with ID = userId
        }
    };

    // you should do this after login (connect) to the Chat
    QBRoster contactsRoster = QBChatService.getInstance().getRoster();

    contactsRoster.setSubscriptionMode(QBRoster.SubscriptionMode.mutual);
    contactsRoster.addSubscriptionListener(subscriptionListener);
    contactsRoster.addRosterListener(rosterListener);
    ```
  </Tab>

  <Tab title="Kotlin">
    ```Kotlin theme={null}
    val rosterListener = object : QBRosterListener {
        override fun entriesDeleted(collection: Collection<Int>?) {

        }

        override fun entriesAdded(collection: Collection<Int>?) {

        }

        override fun entriesUpdated(collection: Collection<Int>?) {

        }

        override fun presenceChanged(presence: QBPresence?) {

        }
    }

    val subscriptionListener = QBSubscriptionListener { userId: Int -> {
        // subscription was requested by user with ID = userId
        }
    }

    // you should do this after login (connect) to the Chat
    val contactsRoster = QBChatService.getInstance().roster

    contactsRoster.subscriptionMode = QBRoster.SubscriptionMode.mutual
    contactsRoster.addSubscriptionListener(subscriptionListener)
    contactsRoster.addRosterListener(rosterListener)
    ```
  </Tab>
</Tabs>

The following table lists all supported events of the `QBRosterListener`.

| Event             | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| entriesDeleted()  | A contact list item is removed from the contact list.                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| entriesAdded()    | A new contact list item is added to the contact list.                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| entriesUpdated()  | A contact list item is updated. For example, a user's name can be updated.                                                                                                                                                                                                                                                                                                                                                                                                                 |
| presenceChanged() | A user presence is changed. Care should be taken when using the presence data delivered as part of this event. Specifically, when a user account is online with multiple devices, the UI should account for that. For example, say a user is online with their desktop computer and mobile phone. If the user logs out of the instant messaging client on their mobile phone, the user should not be shown in the contact list as offline since they're still available on another device. |

<Tip>
  You should call `chatService.getRoster()` only after successful connecting to Chat. To learn how to it, review [this section](/sdks/ios-chat-connection#connect-to-chat-server).

  `QBRosterListener` is a listener that is fired any time a contact list is changed or the presence of a user in the contact list is changed (for example, user becomes online/offline).

  `QBSubscriptionListener` is a listener that is called `subscriptionRequested` when the current user has been requested for adding to the Contact List from any user.
</Tip>

## Access contact list

The following function gives you access to all contact list items.

<Tabs>
  <Tab title="Java">
    ```Java theme={null}
    Collection<QBRosterEntry> entries = contactsRoster.getEntries();
    ```
  </Tab>

  <Tab title="Kotlin">
    ```Kotlin theme={null}
    val entries = contactsRoster.entries
    ```
  </Tab>
</Tabs>

A `QBRosterEntry` describes a user entity in your contact list. To get the Presence of a user, use `getPresence(userID)` method. Then, you can get a user's status.

<Tabs>
  <Tab title="Java">
    ```Java theme={null}
    QBPresence presence = contactsRoster.getPresence(user.getId());

    if (presence == null) {
        // no user in your contact list
        return;
    }
    if (presence.getType() == QBPresence.Type.online) {
        // user is online
    } else {
        // user is offline
    }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```Kotlin theme={null}
    val presence = contactsRoster.getPresence(user.id)

    if (presence == null) {
        // no user in your contact list
        return
    }
    if (presence.type == QBPresence.Type.online) {
        // user is online
    } else {
        // user is offline
    }
    ```
  </Tab>
</Tabs>

## Add user to your contact list

To add a user to the contact list, use the following snippet.

<Tabs>
  <Tab title="Java">
    ```Java theme={null}
    try {
        contactsRoster.subscribe(user.getId());
    } catch (SmackException.NotConnectedException exception) {

    }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```Kotlin theme={null}
    try {
        contactsRoster.subscribe(user.id)
    } catch (exception: SmackException.NotConnectedException) {

    }
    ```
  </Tab>
</Tabs>

After that, the User you have just subscribed to, receives a callback `subscriptionRequested` in `QBSubscriptionListener`. This is a request to be added to the contact list.

<Tabs>
  <Tab title="Java">
    ```Java theme={null}
    // QBSubscriptionListener

    // ...

    @Override
    public void subscriptionRequested(int userId) {

    }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```Kotlin theme={null}
    // QBSubscriptionListener()

    // ...

    @Override
    public void subscriptionRequested(int userId) {

    }
    ```
  </Tab>
</Tabs>

<Note>
  Maximum number of contacts is 300.
</Note>

## Confirm the contact request

To confirm the request, use `contactsRoster.confirmSubscription()` method.

<Tabs>
  <Tab title="Java">
    ```Java theme={null}
    try {
        contactsRoster.confirmSubscription(user.getId());
    } catch (SmackException.NotConnectedException | SmackException.NotLoggedInException | SmackException.NoResponseException | XMPPException exception) {

    }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```Kotlin theme={null}
    try {
        contactsRoster.confirmSubscription(user.id)
    } catch (exceptione: SmackException.NotConnectedException) {

    } catch (exceptione: SmackException.NotLoggedInException) {

    } catch (exceptione: SmackException.NoResponseException) {

    } catch (exception: XMPPException) {

    }
    ```
  </Tab>
</Tabs>

## Reject the contact request

To reject the request, use the `contactsRoster.reject()` method.

<Tabs>
  <Tab title="Java">
    ```Java theme={null}
    try {
        contactsRoster.reject(user.getId());
    } catch (SmackException.NotConnectedException exception) {

    }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```Kotlin theme={null}
    try {
        contactsRoster.reject(user.id)
    } catch (exception: SmackException.NotConnectedException) {

    }
    ```
  </Tab>
</Tabs>

## Remove user from the contact list

To remove a previously added user from the contact list, use the following method.

<Tabs>
  <Tab title="Java">
    ```Java theme={null}
    try {
        contactsRoster.unsubscribe(user.getId());
    } catch (SmackException.NotConnectedException exception) {

    }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```Kotlin theme={null}
    try {
        contactsRoster.unsubscribe(user.id)
    } catch (exception: SmackException.NotConnectedException) {

    }
    ```
  </Tab>
</Tabs>

## Contact list updates

The above-specified `RosterListener` listener will give you all updates regarding contact list changes and users' status updates.
