powered by Jive Software

How to join chatoom room automatically using smack

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));
					mMultiUserChat.addInvitationRejectionListener(this);
					groupchats.put(mGroupChatName, mMultiUserChat);
				}
				DiscussionHistory history = new DiscussionHistory();
				history.setMaxStanzas(0);
				mMultiUserChat.join(Resourcepart.from(mNickName),null,history,SmackConfiguration.getDefaultPacketReplyTimeout());
				mMultiUserChat.addMessageListener(new MessageListener() {

					@Override
					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

There is a concept of Bookmarks in XMPP, which is supported by Smack. Each user can maintain its own list of bookmarked rooms. Optionally, you can mark the bookmark as “autojoin”. Maybe you can use this in your implementation.

But for bookmark to work we need to first store the chatrooms details during the first time of joining to chatrooms right?.

But my question is what if user is already joined to list of rooms he got and after that he has been added into a new room as a member, then how he is going to get that event So, that he can able to join(automcatically) , I was referring auto join in this context.

Ah, no, something like that does not automatically exist, as far as I know. What XMPP does support is the concept of ‘invitations’. You could create a bit of server-sided code that generates an invitation for a user for a (new) chatroom that this user has just become a member of. Its client would then presumably show the end-user a popup (or possibly, automatically accept the invitation). There is some room for abuse here, so you probably should be careful, but a concept like this might give you a way to work on your issue.

is there any example code for writing plugin for generating a invitation on adding a particluar participant.

Not that I’m aware of. There might be tests in the Smack Integration Test framework, but those are unlikely to be very educational. The Spark client source code probably contains code to send an invite too, but trying to read that code is probably harder than looking up the relevant code in Smack and its javadoc.