Send Messages only to on-line friends

I’'m tring to use this code to know if a user is on-line or not:

final Roster roster = con.getRoster();

System.out.println(" Roosters ");

for (Iterator i=roster.getEntries(); i.hasNext(); ) {


RosterEntry s = (RosterEntry);





}catch (Exception ex){





but the s.getStatus() always return me “null” to all users in my roster.

Am I wrong in the code? Or maybe is not this the right way to do this?

Thanks for your help,


Are you really telling me that no-one here could manage if a user is on-line or not?

If you took a look at the JavaDocs, you would see that RosterEntry#getStatus has nothing to do with the current state of a clients Presence.

Why not take a look at Presence and in particular Presence.Mode

You should find what you need there



Ok, thanks for your help. I see I was wrong, now I’'m actually using this code, that still wont work as I espect:

final Roster roster = con.getRoster();

System.out.println("-- Roosters --");

for(Iterator j = roster.getGroups(); j.hasNext();){

RosterGroup rg = (RosterGroup);

System.out.println("Group name: " + rg.getName());

System.out.println("Group count: " + rg.getEntryCount());


Iterator<RosterEntry> rei = rg.getEntries();


RosterEntry re =;

System.out.println("name:… " + re.getName());

System.out.println("user:… " + re.getUser());

System.out.println("subscription status:. " + re.getStatus());

System.out.println("subscription type:… " + re.getType());

Presence p = roster.getPresence(re.getUser());


System.out.println("presence:… " + p.getMode());




p is always null, also if users are on-line. Am I still wrong in anything?

Roster.getPresence will return null if the user is offline at the time of the call, or if you are not currently subscribed to that users Presence

If the user is online and you are correctly subscribed to thier presence, then it maybe because your code is running to quickly after you authenticate against the server.

For sake of testing, try and put a Thread#sleep inbetween logging in and calling the Roster code.



Great, now it works…

But I also think taht is really strange to have to put the thread to slep to make it worinkg…

Its not that strange tbh. Dont forget that when you call XMPPConnection#getRoster you are requesting information across the wire which may or may not have been populated with the correct results.

If you want to ensure your Roster stays up-to-date add a RosterListener to your app. This will then listen out for Roster changes and you can update your UI accordingly


I,m still using the RosterListener, but I don’‘t need a UI. I just need to know who’'s on-line when a kind of boot wake up. It needs to know this information to send messages just to who is on line at the moment.

Your suggestion works good for now. Thankyou very much!