Is the code correct for RosterListener and Presence?

Hi,

I have written a code using smack API to login to my Openfire server and print all the contacts / buddy / Roster and also want to print the presence information of each user. I am able to login and print all the contacts and working OK.

But some times presence works and sometimes not. Also after successfull connection when i change the status of a perticular contact that “presence change” / “status change” doesn’t reflect in the Rosterlistener. (Changed to Busy From Available)

Is it possible to print the status messages from the contacts?

Can any body please rebview the code and confirm me that the code is correct?

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import javax.imageio.ImageIO;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.;
import org.jivesoftware.smackx.packet.VCard;
import org.jivesoftware.smack.
;

public class IMClient {

// ++++++++++++++++++ RosterListener ++++++++++++++++++++++++++++ //

public static void CreateRosterListener(Roster roster)
{
    roster.addRosterListener(new RosterListener() {
    public void entriesAdded(Collection addresses) {}
    public void entriesDeleted(Collection addresses) {}
    public void entriesUpdated(Collection addresses) {}
   
    public void presenceChanged(Presence presence) {
                           
        System.out.println("Presence changed: " + presence.getFrom() + " " + presence+ "\n");
    }
    });
}
// ++++++++++++++++++++ RetreiveRosters +++++++++++++++++++++++++++++++ //
   
    public static void RetreiveRosters(Roster roster,Collection entries)
    {
        Iterator it = entries.iterator();
       
        while(it.hasNext())
        {
            RosterEntry entry  = (RosterEntry) it.next();                           
            System.out.println(entry.getName() + " (" + entry.getUser() + ")  ["+entry.getStatus()+ "] - [" + entry.getType() + "]");                               
        }           
    }

public static void main(String args[])
{
    try
    {
        XMPPConnection connection = new XMPPConnection("Your Server IP");
        connection.connect();
        connection.login("UserName@domain", "Password");
                   
        Roster roster = connection.getRoster();
        Collection entries = roster.getEntries();
       
        CreateRosterListener(roster);
        RetreiveRosters(roster,entries);                                                                                                         
    }
    catch(Exception e)
    {
              System.out.println(e);
    }

}

}

Thanks In Advance

Soumyadipta De

Your code works for me, it will detect the presence changes of users on its roster. Are you sure the user you’re logging in as in this code has proper subscription to the clients on it’s roster (as in subscription status of ‘to’ or ‘both’, if it’s just ‘from’, you won’t get presence updates from those clients).

You can print the status in your roster listener, presence.getStatus() will give you the status. Or during your iteration of your roster entry, if you can get the presence of a buddy (roster.getPresence(entry.getUser()), which will return a presence object, calling getStatus on that will give you the buddy status.

HTH

Dan