How to get a MultiUserChat room that already exists

If one wanted to get a MultiUserChat room that already exists, in order to get the list of Occupants, and one knew the room name could said room and Occupants be retreived like this?

MultiUserChat muc = new MultiUserChat(connection, “existingroom@conference.server”);

Iterator iter = muc.getOccupants();

Or would this create a new room and thus return an empty Iterator?

Thanks in advance


Message was edited by: joeschmoe

If the MultiUserChat (MUC) was created by another user then you must firts join the MUC before getting the Occupants

MultiUserChat muc = new MultiUserChat(conn, "existingroom@conference.server"); muc.join("test"); for (Iterator it = muc.getOccupants(); it.hasNext();) {
    String occupant = (String);

So, let me get this straight. If I didn’'t create it then I must join first but if I created it then I would not need to join?

OK, maybe I misunderstood your question. If you create the MUC or you have already joined to it you must keep it, in for example an array, to get it back. If you create a new instance it will be an “empty” one as you said, no matters if it has the same name as the original. The code that I wrote was about how to join and get the occupants of and “existing” MUC in the server, not how to get one that already “exist” in the smack client.

I am having a similar problem. I am trying to send a file to users in a MultiUserChat room so I am storing the Rooms in a Room Cache and then retreiving the room to get the list of Occupants so that I can send them a file. When I get the room from the cache it works the first time but after that it fails.

Any ideas,


I was looking at the code for the MUC object and the occupantsMap that it uses to store the occupants is only populated by a PacketListener so only users that join the room at a later date get put in the map. It makes no attempt to get the current occupants of the room. So the code that gets the occupants likely be entry if called immediately after joining. If you wait for a bit (a couple seconds) then the only occupant will likely be the one that just joined.

A brief look through the protocols didn’'t show an obvious way to retrieve the list of users in a room so if anyone knows, that would help.

I was able to get the following code to retrieve the list of users in a room from the server directly:

public List getOccupants2() throws XMPPException {
        List answer = new ArrayList();
        ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(connection);
        DiscoverItems items = discoManager.discoverItems(room);
        for (Iterator it = items.getItems(); it.hasNext();) {
            DiscoverItems.Item item = (Item);
        return answer;            }

It didn’‘t work 100%, but i didn’‘t have time to track it down. Could this code be used to load the users initially and then updates come through the listener. It doesn’‘t take into account the presence status of the users, but it’'s a start.

Message was edited by: mlee (formatting)

Yes you are right, the only part of the code that adds occupants is a packetListener, but when you join to an already created room, the servers sends you the presence of all the already joined users, so no matters when you join a room, you will always have all the occupants.

For example this code:

public void test() {
          XMPPConnection conn1 = new XMPPConnection("domain");
          XMPPConnection conn2 = new XMPPConnection("domain");           conn1.login("user1", "pass");
          conn2.login("user2", "pass");           MultiUserChat muc = new MultiUserChat(conn1, "myRoom@conference.domain");
          muc.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));           Thread.sleep(1000);           MultiUserChat muc2 = new MultiUserChat(conn2, "myRoom@conference.domain");
          muc2.join("secondUser");           Thread.sleep(1000);           System.out.println("First muc occupants");
          for (Iterator it = muc.getOccupants(); it.hasNext();) {
          }           System.out.println("Second muc occupants");
          for (Iterator it = muc2.getOccupants(); it.hasNext();) {


First muc occupants



Second muc occupants



Message was edited by: gguardin

What do you mean by “it fails”, is there an exception or the list of occupants is empty? Can you post some code to reproduce it?

it could be the sleeps that were missing from our code. i was wondering if we needed to give the server time to send the messages.

Yes, you must wait some time, the presences are send asynchronously