powered by Jive Software

Roster and Subscribe/Unsubscribe operations

I’‘ve been working with the Smack roster and rosterentry apis in smack and am having a small bit of difficulty. Specifically, it’‘s not at all clear to me what the right sequence of api calls are in smack to implement some of the ‘‘manual’’ subscription policies, as well as add/remove entries from the roster (which frequently have associated with them subscribe/unsubscribe operations). Since the underlying messages are asynchronous, and subscription (e.g.) requires human approval (which can be delayed/never happen, etc), it’‘s not at all obvious what the right sequence of events and/or api actions is. The documentation for smack has only the most basic stuff about roster and presence handling, and I wasn’'t able to find more.

I can/will lookup the xmpp protocols specs for state transition diagrams and such, but this won’‘t make it clear to me what the relationship is between the smack api calls and the generated xmpp messages (which I’'ve been looking at with debugger to understand, but would like to go to a more definitive and less labor-intensive resource).

Please let all know if some more of this documentation exists…and where…even in the form of open source example apps that use the api to implement more complete subscribe/unsubscribe roster add/remove behavior.

Hey Scott,

If you want to manually handle the subscription requests then you have to send this message: Roster.setDefaultSubscriptionMode(Roster.SUBSCRIPTION_MANUAL). As you can see in the comment of the static variable SUBSCRIPTION_MANUAL you will need to register your listener to handle presence requests that have the type Presence.Type.SUBSCRIBE.

That means that you need to do something like this:

// Listen for any presence packets.
        PacketFilter presenceFilter = new PacketTypeFilter(Presence.class);
        connection.addPacketListener(new MyPresencePacketListener(), presenceFilter);

where MyPresencePacketListener will have code like this in the #processPacket method to accept or reject the subscription requests:

if (presence.getType() == Presence.Type.SUBSCRIBE) {
        if (....some logic....) {
            // Accept subscription requests.
            Presence response = new Presence(Presence.Type.SUBSCRIBED);
            response.setTo(presence.getFrom());
            connection.sendPacket(response);
        }
        else if (.... some logic...) {
            // Reject subscription requests.
            Presence response = new Presence(Presence.Type.UNSUBSCRIBED);
            response.setTo(presence.getFrom());
            connection.sendPacket(response);
        }

Hope that helps,

– Gato