How do I subscribe to a user''s presence?

Sorry if this is in the doc and a newbie question (and I suspect it is), but I have not been able to find it. How do I subscribe to a user’'s presence using the Smack API? I am able to add other users to my roster, but cannot get presence information (null).

If I use another Jabber client (Exodus) to request a subscription (simply by adding the other user to my roster), then I am able to successfully retrieve presence information. But have not figured out how to add a user to my roster using Smack and then get their presence.

I would have thought that adding a user to my roster would have generated a subscription request, but apparently not. Please advise.

Again, apologies if this should be obvious from the doc.


The method


will create a new Presence subscription. They will need to then approve the request. Once they do, calling


should work fine



OK, here’‘s what I’'m doing to add a user to my roster:

 public boolean addRoster(String jid) {

 // Get roster for this logged in user
 Roster roster = this.conn.getRoster();

 // Add a roster entry

 try {
 roster.createEntry(jid, "", null);
 } catch (XMPPException e) {
 this.message = e.getMessage();
 return false;

 this.message = null;
 return true;

However, this does not seem to create a ‘‘subscription’’. What am I doing wrong?

Does the JID that gets added need to include the client (e.g., /Smack or /Exodus)? Or just the username@server?



Message was edited by: DFColorNet

using should work fine

Couple of things to check for, make sure you have a valid roster object and also take a look at the debugger. You should see a subscription packet being sent out when you call


OK, I have this working now … the key is apparently in the asynchronous nature of the Smack API.

In my app, the ‘‘Roster Add’’ was being done by a servlet called from a web page; the web page would prompt for the JID to add, user would click submit, the servlet would connect to the Jabber server, get the user’‘s roster, add the JID, and then disconnect. This was apparently too quick. I’'ve added a 5 second delay before disconnecting, and now I reliably get subscription requests.

Just curious about a recommendation. What is a ‘‘reasonable’’ amount of time to wait when performing Smack API calls like this? I ran into a similar problem earlier, trying to get the status of all users on the roster. Initially, everyone was coming back as offline, until I added a 500ms delay. Now I get the correct status back (assuming, of course, that I am subscribed to the other user’'s presence and they have accepted). Assuming no significant delays on the Jabber server itself (and understand this is a big assumption), should most async calls complete with 500 ms? 1 second? 5 seconds? Looking for a rule of thumb to go by.