#StackBounty: #xmpp #openfire #smack How to join chatoom room automatically using smack

Bounty: 50

Using smack I am creating connection(usera), and after that I am joining to the chatrooms by getting the list of chatrooms. But after this if another user(userb) adds the usera into a new chatroom. But how can I automatically join to the newly created chatroom?

Below is the code to get list of chatrooms and joining chatroom. But in the list I am getting all public chatrooms. How can I get the list of chatrooms in which usera is participant/memeber?

List<DomainBareJid> mucServiceDomains = connection.mucManager.getMucServiceDomains();

       //get list of chatrooms
                for (DomainBareJid domainBareJid : mucServiceDomains) {
                    List<HostedRoom> hostedRooms = connection.mucManager.getHostedRooms(domainBareJid);
                    for (HostedRoom hostedRoom : hostedRooms) {
                        Log.info("hostedRooms::: jid{}  name{}",hostedRoom.getJid(),hostedRoom.getName());
                        try {
                            connection.joinRoom(hostedRoom.getJid().toString(), connection.getUserJid().toString());
                        } catch (Exception e) {
                            Log.error("Error joinign muc::{}",e);

       // joinroom by room name
    public boolean joinRoom(String mGroupChatName, String mNickName) {
        Log.debug("joinRoom " + mGroupChatName + " " + mNickName);
        if(mGroupChatName!=null) {
            try {
                MultiUserChat mMultiUserChat = groupchats.get(mGroupChatName);

                if (mMultiUserChat == null)
                    mMultiUserChat = mucManager.getMultiUserChat(JidCreate.entityBareFrom(mGroupChatName));
                    groupchats.put(mGroupChatName, mMultiUserChat);
                DiscussionHistory history = new DiscussionHistory();
                mMultiUserChat.addMessageListener(new MessageListener() {

                    public void processMessage(Message message) {
                        Log.info("MUC message recieved is::{}",message.getBody());

                return true;

            } catch (Exception e) {
                Log.error("joinRoom", e);

        return false;

Smack version: 4.2.1

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.