Java – smack status listener in multi-user chat

smack status listener in multi-user chat… here is a solution to the problem.

smack status listener in multi-user chat

The smack status listener in a multi-user chat is not called. Log in using the Smack API and add roster.addRosterListener(mRoasterListener); However, when the presence of other users in the chat room changes, it cannot be successfully listened to. I tried the following code to get the presence listener to work:

connection.login(loginUser, passwordUser);

MultiUserChatManager manager = 

MultiUserChatManager.getInstanceFor(connection);

muc = manager.getMultiUserChat(roomID + "@" +context.getString(R.string.group_chat_id));

Log.d("Join User: ", "Already Created");

muc.join(Utilities.getUserPhoneNo(context));

muc.addMessageListener(mGroupMessageListener);

Roster roster = Roster.getInstanceFor(connection);//luna

roster.addRosterListener(mRoasterListener);//roasterListener

Log.d("Joined User Phone: ", " " + Utilities.getUserPhoneNo(context));

This class is used to listen for changes in presence….

public class RoasterListener implements RosterListener{
        public RoasterListener(Context context){

}

@Override
        public void entriesAdded(Collection<String> collection) {

}

@Override
        public void entriesUpdated(Collection<String> collection) {

}

@Override
        public void entriesDeleted(Collection<String> collection) {

}

@Override
        public void presenceChanged(Presence presence) {
            System.out.println("Presence changed: " + presence.getFrom() + " " + presence);
        }
    }

I tried many of the links provided by StackOverflow without success.
Please help!

Solution

For multi-user chats, you don’t have to use a roster because it’s normal to meet someone who isn’t on the roster.

To know who is in the MUC, first ask the occupants:

muc.join(user,password);

List<String> occupantsAtJoinTime = muc.getOccupants();

for (String occupant : occupantsAtJoinTime)
                    {
                        System.out.println("occupant: "+occupant);
                        actions
                    }

Then, to update the Occupants list, register a DefaultParticipantStatusListener with your muc and define the Listner:

muc.addParticipantStatusListener(new CustomParticipantStatusListner());

Defined as (there are many ways to implement it as needed):

    public class CustomParticipantStatusListner extends DefaultParticipantStatusListener 
    {

public void joined(String participant) 
        {
            System.out.println(participant + "just joined MUC");
actions (add occupantsRightNow)
        }

public void left(String participant)
        {
            System.out.println(participant + " just left MUC");
actions (remove occupantsRightNow)
        }
    }

All this with smack 4.1.7

Related Problems and Solutions