powered by Jive Software

Can one get all resources and presences for a given user from the roster?

  1. Is it possible to get all resources for a given user from the Roster?

For Smack 2.2.0 the javadoc says

for org.jivesoftware.smack.Roster.getPresence(String user)

''public Presence getPresence(String user)

Returns the presence info for a particular user, or null if the user is unavailable (offline) or if no presence information is available, such as when you are not subscribed to the user’'s presence updates.

If the user has several presences (one for each resource) then answer the presence with the highest priority.

But is there any way to get all presences and all resources for a given user from the Roster

me@mycompany.com/home might be available

me@mycompany.com/work might be away

  1. If answer to 1 is no, I am assuming I could listen on org.jivesoftware.smack.XMPPConnection and get all this information with a presence message. Would this work?



From the Javadocs

The method below Roster#getPresence(String user) is

Roster#getPresences(String user)

Returns an iterator (of Presence objects) for all the user’‘s current presences or null if the user is unavailable (offline) or if no presence information is available, such as when you are not subscribed to the user’'s presence updates.

Should help you out I think


For 2 I am talking about inbound messages which will come in after the user has logged on

OK. Thanks Jon. I’'m obviously partially sighted.

What I am actually trying to do is

  1. get full jabber ids (including resource)

  2. get presence (available/busy etc…) for that full jabber id

  3. get group for that full jabber id

I still don’'t know how to do 1, 2 and 3 using the smack Roster



Something like this should be enough to get you started

// Get all Entries from your Roster

Iterator allEntries = Roster.getEntries();

//loop through and get each JID


RosterEntry rosterEntry = (RosterEntry)allEntries.next();

// 1: Calling rosterEntry.getUser() will return the fully qualified JID

Presence presence = Roster.getPresence(rosterEntry.getUser());

// 2: You now have the presence for that user

Iterator groups = rosterEntry.getGroups();

// 3: You now have all the Groups that the JID belongs to

while(groups.hasNext()) {

RosterGroup rosterGroup = (RosterGroup)groups.next();





Hi Jon,

I have started

I am doing something similar already.

The problem seems be that RosterEntry does not give me the ‘‘full’’ jabber id (with resource), as far as I know, only Presence.getFrom() seems to do this.

Your step 3 will get the groups for me@mycompany.com,


I am trying to get the group/groups for

me@mycompany.com/work - work group


me@mycompany.com/home - play and work at home groups

I don’'t expect you spend any more time looking at this for me. It looks like I have will have to bypass the Roster & listen directly on XMPPConnection, so that I see all messages going to and fro.

Thanks for the help,


Did I quick forum search and stumbled across this

The presence objects from getPresences(String) includes the resource as the “from” field in the packet. You could use the following code as an example:

Iterator iter = roster.getPresences("user@example.com");

while (iter.hasNext()) {

Presence presence = iter.next();

System.out.println("Full JID: " + presence.getFrom());



With this you could then call RosterEntry.getGroups(presence.getFrom())

With this you could then call


Hi Jon,

There isn’'t a static method like this.

Once you have an instance of RosterEntry, you seem to have an object which has no knowledge of resource(s).

Calling rosterEntryObject.getGroups won’'t give you the link between full jabber id(including a resource) and group(s) that I looking for.

Smack seems to be very well put together, and I will be happy to be corrected, but this seems a flaw in the Smack object model.

As I said, I’'d be happy to be corrected.


I’‘m sure someone can correct me if I’'m wrong but is it not impossible to add a contact with a particular resource to a Group?.

I thought that you added a Contact to 0.* more groups and they were then able to login with various different resources should they wish. You have no control over that.

I could be wrong though.


That would seem to make sense. I was wondering if that was the case.

So to continue the example

If me@mycompany.com is ‘‘member’’ of the work and the play groups.




and we wanted to display in roster by group, all 3 resources (and any other resources of me@mycompany.com) should be always be displayed in both the work and the play groups.

I think you are right but if I could find a JEP or similar standard that confirms this, that would be perfect.