Multi User Chat with (MUC,Group Chat ) Openfire.
- Digital Engineering
Multi User Chat with (MUC,Group Chat ) Openfire.
Openfire provides multiuser chat using smack lib in android. In the previous blog, i have explained connection creation.We need to follow the following the step to configure MUC.
- Initialisation Connection
- Create a new Room
- Join a room
- Manage room invitations
- Discover joined rooms
- Discover room information
- Start chat in group
Initialisation Connection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
public void initConnection() { if (connection == null || !connection.isConnected()) { try { Log.d("CONNECT", "Trying to conenct with username " + username + " and pasword " + password); XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder(); configBuilder.setUsernameAndPassword(username, password); //configBuilder.setResource("SomeResource"); configBuilder.setServiceName(chatServer); configBuilder.setHost(chatServer); configBuilder.setPort(5222); //configBuilder.setDebuggerEnabled(true); configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled); connection = new XMPPTCPConnection(configBuilder.build()); connection.connect(); connection.setPacketReplyTimeout(100000); Log.d("CONNECT", "Trying to Login " + username + " password " + password); connection.login(username, password); Log.d("CONNECT", "Trying to message"); Presence presence = new Presence(Presence.Type.available); connection.sendPacket(presence); PacketFilter filter = MessageTypeFilter.GROUPCHAT; connection.addSyncPacketListener(new PacketListener() { @Override public void processPacket(Packet packet) throws SmackException.NotConnectedException { Message message = (Message) packet; Log.d("Debugging", "Message3: " + message.toString()); final com.avi.ejjabberd.dto.Chat chat = new com.avi.ejjabberd.dto.Chat(); chat.setFrom(message.getFrom()); chat.setMessage(message.getBody()); DelayInformation inf2 = (DelayInformation) message.getExtension("delay", "urn:xmpp:delay"); if (inf2 != null) { chat.setTime(getConvertedTime(inf2.getStamp())); } else { chat.setTime(new Date()); } chat.setId(message.getPacketID()); chat.setTo(ChatAdaptor.parseTo(message.getFrom())); chatListener.processMessage(chat); Log.d("CONNECT", "Chat to " + chat.getTo()); Log.d("CONNECT", "Message from: " + message.getFrom() + " to: " + message.getTo() + " " + message.getBody()); } }, filter); } catch (SmackException e) { Log.d("CONNECT", "Error in connecting to chat server ", e); } catch (IOException e) { Log.d("CONNECT", "Error in connecting to chat server ", e); } catch (XMPPException e) { Log.d("CONNECT", "Error in connecting to chat server ", e); } } } |
Create a Room
There are two types of rooms that you can create.
Instant rooms which are available for immediate access and are automatically created based on some default configuration.
1 2 3 4 5 6 7 8 9 |
MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection); MultiUserChat muc = manager.getMultiUserChat("myroom@conference.jabber.org"); // Create the room muc.create("testbot"); // Send an empty room configuration form which indicates that we want // an instant room muc.sendConfigurationForm(new Form(DataForm.Type.submit)); |
Reserved rooms- which are manually configured by the room creator before anyone is allowed to enter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
MultiUserChat muc = =manager.getMultiUserChat("myroom@conference.jabber.org"); // Create the room muc.create("testbot"); Form form = muc.getConfigurationForm(); // Create a new form to submit based on the original form Form submitForm = form.createAnswerForm(); for(Iterator fields = form.getFields(); fields.hasNext();) { FormField field = (FormField) fields.next(); if (!FormField.type.hidden.equals(field.getType()) && field.getVariable() != null) { // Sets the default value as the answer submitForm.setDefaultAnswer(field.getVariable()); } } // Sets the new owner of the room List owners = new ArrayList(); owners.add("johndoe@jabber.org"); submitForm.setAnswer("muc#roomconfig_roomowners",owners); muc.sendConfigurationForm(submitForm); |
Join the Room- Your usual first step in order to send messages to a room is to join the room. Multi User Chat allows to specify several parameter while joining a room. Basically you can control the amount of history to receive after joining the room as well as provide your nickname within the room and a password if the room is password protected.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public boolean join(String group) { try { initConnection(); MultiUserChatManager mchatManager = MultiUserChatManager.getInstanceFor(connection); mchat = mchatManager.getMultiUserChat(group + "@" + groupChatServer); if (!mchat.isJoined()) { Log.d("CONNECT", "Joining room !! " + group + " and username " + username); boolean createNow = false; try { mchat.createOrJoin(username); createNow = true; } catch (Exception e) { Log.d("CONNECT", "Error while creating the room " + group + e.getMessage()); } if (createNow) { mchat.sendConfigurationForm( new Form(DataForm.Type.submit)); //this is to create the room immediately after join. } } Log.d("CONNECT", "Room created!!"); return true; } catch (SmackException e) { e.printStackTrace(); } catch (XMPPException.XMPPErrorException e) { e.printStackTrace(); } return false; } |
Discover joined rooms
1 2 3 |
MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection); // Get the rooms where user3@host.org has joined List<String> joinedRooms = manager.getJoinedRooms("user3@host.org/Smack"); |
Start chat in group
1 2 3 4 |
public void sendMessage(String message) throws XMPPException, SmackException.NotConnectedException { mchat.sendMessage(message); } |
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s