XEP-0144: Roster Item Exchange


XEP-0144: Roster Item Exchange allows you to exchange contacts with other contacts.

Although there’s also the possibility to suggest whether a contact is to be deleted or modified, a client should only be allowed to suggest contacts to be added.

Suggesting a Contact to Be Added

This will suggest the contact “Juliet” to be added to “Romeo’s” roster:

ContactExchangeManager contactExchangeManager = xmppClient.getManager(ContactExchangeManager.class);
contactExchangeManager.suggestContactAddition(Jid.of("romeo@example.net"), new Contact(Jid.of("juliet@example.net"), "Juliet"));

Listening for Contact Exchange Suggestions

If you want to support contact exchange and therefore indicate support for it you should first enable the manager. Once enabled, inbound contact exchange suggestions can be listened to by adding a listener to the manager. The event object will contain the suggested items.

final ContactExchangeManager contactExchangeManager = xmppClient.getManager(ContactExchangeManager.class);
contactExchangeManager.setEnabled(true);
contactExchangeManager.addContactExchangeListener(e -> {
    for (ContactExchange.Item item : e.getItems()) {
        // ... Here are some items for you
    }
});

Approving Contact Exchange Suggestions

You can easily approve suggestions by using the approve method:

contactExchangeManager.approve(item);

In case the contact was suggested to be added, this will add the suggested contact to your roster and subscribe to its presence. If it already exists in your roster but in another group, the contact will additionally be added to the suggested group.