powered by Jive Software

Manage subscription

Hi guys,

I have an application working quite well using smack.

But I need to manage subscription.

I don’'t get the point about SUBSCRIPTION_MANUAL.

I have a listener for presence packets, but I never got a presence subscription information whereas I have presence notification working well.

SO my question is can somebody explain me (or show me with an example sample) how :

  • to ask for subscription to others (Packet)

  • to catch subscription requiement from others

Thanks

To ask a subscription I have:

roster = new RosterPacket();

roster.setType(IQ.Type.SET);

roster.addRosterItem(

new RosterPacket.Item(jid, nick));

connection.sendPacket(roster);

presence = new Presence (Presence.Type.SUBSCRIBE);

presence.setTo(jid);

connection.sendPacket(presence);

To catch a subscription ask, I sorry but haven’'t implemented yet!

Thanks for your help.

Your code doesn’'t seem to work properly.

It does work fine for you?

+/- lol.

It works, well it send’‘s all has supposed to send, but the problem is the server (OpenIM) that raises a exception! But that I think it’'s a bug…

Nuno Agapito

ok, this seems to work fine for me.

But does anybody know how I can be notified for a subscription request from others?

thanks

ofupien,

Changing the Roster to use SUBSCRIPTION_MANUAL means that you must register a listener for presence packets. The listener will have to look for any presence requests that have the type Presence.Type.SUBSCRIBE.

Sample code:

// Listen for any presence packets.
    PresencePacketListener myPresencePacketListener = new PresencePacketListener() {
        public void processPacket(Packet packet) {
            Presence presence = (Presence)packet;
            String from = presence.getFrom();
            String key = StringUtils.parseBareAddress(from);
            if (presence.getType() == Presence.Type.SUBSCRIBE) {
                // Custom logic
                // Accept or Reject subscription requests.
            }
        }
    };
    PacketFilter presenceFilter = new PacketTypeFilter(Presence.class);
    connection.addPacketListener(myPresencePacketListener, presenceFilter);

Regards,

– Gato

thank you Gato for your code sample.

But then I have a question:

In fact, I did something similar to what you’'ve done.

I haven’‘t tried yours yet, but Mine didn’'t work certainly because I used a RosterListener and not a PresenceListener.

Using my RosterListener, I manage well my contacts presence. But It didn’'t work with the TYPE.SUBSCRIBE.

SO my question is what is the difference between RosterListener and PresenceListener?

thanks.

SO my question is what is the difference between

RosterListener and PresenceListener?

RosterListener only listens for presence updates for people in your roster. Basically, it’'s there to allow you to easily monitor people already in your roster. A PresenceListener can listen for all presence packets, including subscribe packets.

Regards,

Matt

ofupien,

I made a copy&paste mistake. I meant PacketListener instead of PresencePacketListener.

Differences:

A PacketListener must be added to an XMPPConnection while a RosterListener must be added to a Roster.

A PacketListener listens for all the packets received by an XMPPConnection or specified by a PacketFilter while a RosterListener listens for RosterPackets.

Since you are interested in Presence packets you should use a PacketListener instead of a RosterListener.

Regards,

– Gato

Thank your precious help

I meant thanks for your precious help