Question

I'm creating chat application using QuickBlox API, From the sample app i can get all list users in my account.But i want to show only the online available users. How can i retrieve the online available users?

Was it helpful?

Solution

I can get the online users of the QuickBlox Chat Module through the SMACK API, using Rooster of a XMPP Connection. Check this answer.

Can I get online users in my friend list via Smack?

You need the online users for Chat Module or for other one?

Cheers!

OTHER TIPS

Try this for online available users by using smack API. Roster roster = xmppConnection.getRoster();

  Collection<RosterEntry> entries = roster.getEntries();
  Presence presence;

    for(RosterEntry entry : entries) {
        presence = roster.getPresence(entry.getUser());

        System.out.println(entry.getUser());
        System.out.println(presence.getType().name());
        System.out.println(presence.getStatus());
    }

Check whether user is online(or) offline

  Presence presence = roster.getPresence("tom@jabber.org");
   if (presence.getType() == Presence.Type.AVAILABLE) {
       // Tom is online...
        }

By using presence.getMode() method to get Mode of User. Mode is enum and its value can be chat, available, away, xa, dnd.

XMPPConnection.addConnectionCreationListener(new ConnectionCreationListener() {

    @Override
    public void connectionCreated(Connection arg0) {
        Log.i(TAG, "receive xmpp connection : " + arg0);
        connection = arg0;
        roster = arg0.getRoster();

        Collection<RosterEntry> entries = roster.getEntries();
        Presence presence;

        Log.e(TAG, "user count" + entries.size());

        for (RosterEntry entry : entries) {
            presence = roster.getPresence(entry.getUser());

            Log.i(TAG, "" + entry.getUser());
            Log.i(TAG, "" + presence.getType().name());
            Log.i(TAG, "" + presence.getStatus());
        }

    }
});

You can do it as below:

1- Define a roster

  private SmackAndroid smackAndroid;
  private Roster roster;

2- Inside onCreate, initialize the smackAndroid and add XMPPConnectionListener

  smackAndroid = SmackAndroid.init(this);
  XMPPConnection.addConnectionCreationListener(new ConnectionCreationListener() {

    @Override
    public void connectionCreated(Connection connection) {
      roster = connection.getRoster();

    }
  });

3- You can add user to the roster using createEntry

  try {
    roster.createEntry("UserId", "userName", new String[] { "Friends" });
  } catch (XMPPException e) {
    e.printStackTrace();
  }

4- You can get Entries form roster using getEntry()

  ArrayList<String> friends = new ArrayList<String>();
  Collection<RosterEntry> entries = roster.getEntries();
  for (RosterEntry entry : entries) {
    friends.add(entry.getName());
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top