powered by Jive Software

How to list all users of all chat rooms?

Hi,

I get another challenge… I am trying to get a list of all the online users that have joined one of the available MUC. I coded it as follow:

for(int i=0;i<rooms.length;i+) { //rooms is a String[] of room’'s name

room = new MultiUserChat((XMPPConnection)sessionInfos.getConnection(),

sessionInfos.getRoomID(rooms[ i ]));

try {

roomParticipants = room.getParticipants(); //roomParticipants is a Vector

iti = roomParticipants.iterator();

while(iti.hasNext()) {

//participant is a Vector

participants.add(((Occupant)iti.next()).getNick() + " ("

  • StringUtils.parseName(room.getRoom()) + “)”);

}

} catch (XMPPException e) {

Debug.warn("cannot list participants in "

  • StringUtils.parseName(room.getRoom()));

e.printStackTrace();

}

}+

But it returns no user’'s name in participants and throws this exception:

+(403)

at org.jivesoftware.smackx.muc.MultiUserChat.getOccupants(MultiUserChat.java:1704)

at org.jivesoftware.smackx.muc.MultiUserChat.getParticipants(MultiUserChat.java:16 71)+

I have tried to join the MUC before listing the participants but the result was the same… Did I do something wrong? Is there another way to obtain this list?

Thanks,

Seb

PS: sorry for being so inefficient this week… I have the feeling of flooding this poor forum with stupid questions!

Hey Seb,

The 403 error means that the required operation is forbidden for the user that tried to make it. The problem is that #getParticipants() is only allowed to be executed by room owners.

Anyway, #getParticipants() is not what you need to get the online users that have joined a MUC room. That method will return the list of users that have an affiliation of participant (no matter whether they are online or not).

To get the list of occupants you have two ways.

  1. If the room is a Public room (i.e. is listed in the directory) then you can use Service Discovery to get the room occupants. You will need to execute something like this:
// Discover the list of items (i.e. occupants in this case) related to a room
    DiscoverItems result = ServiceDiscoveryManager.getInstanceFor(connection).discoverItems("room@service");
    // Iterate on the result''s items
    for (Iterator items=result.getItems(); items.hasNext();) {
        ....
    }
  1. Join the room and then send #getOccupants() to get the list of occupants. Note that the list will contain not the real JID of the user but the room JID. So you can then send #getOccupantPresence(String) to get the real presence of the user which may (depending on the room configuration) include the real JID of the user.

Regards,

– Gato

Thanks Gato,

It works better with your code . I have read the JEP-0045 and understood my mistake… These affiliation and role concepts are very tricky, it is not very simple to know what are a user’'s privileges.

Regars,

Seb