Home/Multi User Chat with (MUC,Group Chat ) Openfire.

Multi User Chat with (MUC,Group Chat ) Openfire.

Published On: 25 January 2017.By .
  • Product & platform Engineering

 

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

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.

 

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.

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.

 

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

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 

public void sendMessage(String message)
    throws XMPPException, SmackException.NotConnectedException {
  mchat.sendMessage(message);
}

 

Related content

We Love Conversations

Say Hello
Go to Top