Before you begin

  1. Register a QuickBlox account. 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 our Setup page for more details.
  3. Create a user session to be able to use QuickBlox functionality. See our Authentication page to learn how to do it.
  4. Connect to the Chat server. See our Connection page to learn how to do it.

Visit our Key Concepts page to get an overall understanding of the most important QuickBlox concepts.

Subscribe to contact presence updates

You can listen to the contact presence updates in real-time using the QBRosterListener. However, you can listen to the presence of only those users who have been added to the contact list. See this section to learn how to implement the Contact List.

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

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) {
        if (presence == null) {
            // no user in your contact list
            return;
        }
        // if a user uses several devices, you need to do additional check for presence
        QBPresence qbPresence = contactsRoster.getPresence(presence.getUserId());

        if (qbPresence.getType() == QBPresence.Type.online) {
            // user is online
        } else {
            // user is offline
        }
    }
};

contactsRoster.setSubscriptionMode(QBRoster.SubscriptionMode.mutual);
contactsRoster.addRosterListener(rosterListener);

Ping user

QuickBlox SDK can send application-level pings to a user. As a result, you can check if the user is connected to the Chat server.

QBPingManager pingManager = QBChatService.getInstance().getPingManager();
Integer userId = 6453;

pingManager.pingUser(userId, new QBEntityCallback<Void>() {
    @Override
    public void onSuccess(Void aVoid, Bundle bundle) {

    }

    @Override
    public void onError(QBResponseException exception) {

    }
});
ArgumentRequiredDescription
userIDyesID of the user.

Ping server

QuickBlox SDK can send application-level pings to a server. As a result, you can check if there is a connection with the Chat server.

QBPingManager pingManager = QBChatService.getInstance().getPingManager();

pingManager.pingServer(new QBEntityCallback<Void>() {
    @Override
    public void onSuccess(Void aVoid, Bundle bundle) {

    }

    @Override
    public void onError(QBResponseException exception) {

    }
});

Add ping listener

Your pings can be unsuccessful. To handle unsuccessful pings, use the PingFailedListener. It enables your app to listen to failed ping attempts.

QBPingManager pingManager = QBChatService.getInstance().getPingManager();

PingFailedListener pingFailedListener = new PingFailedListener() {
    @Override
    public void pingFailed() {

    }
};

// add PingManager when you need it
pingManager.addPingFailedListener(pingFailedListener);

// remove PingManager when you no longer need it
pingManager.removePingFailedListener(pingFailedListener);