Tuesday, July 23, 2013

MultiUserChat with XMPP - Android Tutorial






In this post I will show you how to build an application with multi user chat running on XMPP server.

For a small introduction read this post: Introduction to xmpp
Now let's go fucking coding!
You can download the Jar file from here

Step 1 - Define some parameters

    public static final String HOST = "jabber.cz";
    public static final int PORT = 5222;
    public static final String SERVICE = "jabber.cz";

    String mNickName;
    String mGroupChatName;
    private MultiUserChatmMultiUserChat;
    private XMPPConnectionmXmppConnection;
    private ConnectionConfiguration mConnectionConfiguration;

Step 2 - Create a connection

 mConnectionConfiguration = 
new ConnectionConfiguration(HOST, PORT, SERVICE);
mConnectionConfiguration.setRosterLoadedAtLogin(false);
mXmppConnection = new XMPPConnection(mConnectionConfiguration);
mConnectionConfiguration.setSASLAuthenticationEnabled(true);
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
mXmppConnection.connect();

Step 3 - Login and set status

mXmppConnection.login(USERNAME, PASSWORD, NICK_NAME);
// Set the status to available
Presence presence = new Presence(Presence.Type.available);
mXmppConnection.sendPacket(presence);

Step 4 - Set the Mutliuser Chat

// Create a MultiUserChat using a Connection for a room 
// (room name as the second parameter)
mMultiUserChat = new MultiUserChat(mXmppConnection, mGroupChatName);
mMultiUserChat.create(mNickName);
mMultiUserChat.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
mMultiUserChat.join(mNickName);

Step 5 - Received messages

// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);
mXmppConnection.addPacketListener(new PacketListener()
{
    @Override
    public void processPacket(Packet packet) 
    {
        Message message = (Message) packet;
        if (message.getBody() != null)
        {
            String from = message.getFrom();
            String Body = message.getBody();
            // Add incoming message to the list view or similar
        }
    }
}, filter);

Step 6 - Send message

// import org.jivesoftware.smack.packet.Message;
Message msg = new Message(to, Message.Type.groupchat);
msg.setBody(msgText);
mMultiUserChat.sendMessage(msg);

45 comments:

  1. Hi Silvia, I have no time to make a sample project and upload it.
    To use the above code, download the SMACK Jar file:
    https://code.google.com/p/asmack/downloads/detail?name=asmack-issue15.jar&can=2&q=

    ReplyDelete
  2. Nice article David. Thanks for the elaborated description.

    ReplyDelete
  3. Thanx David.It's really very helpful.Thank u so much.

    ReplyDelete
  4. Hi @David, i followed the same process u mentioned, but m stuck. Here is what m getting :

    11-23 15:58:54.580: W/System.err(16749): No response from server.:
    11-23 15:58:54.610: W/System.err(16749): at org.jivesoftware.smackx.muc.MultiUserChat.create(MultiUserChat.java:359)

    ReplyDelete
    Replies
    1. Maybe the "jabber.cz" server is down, try to find a new server with the same functionality

      Delete
    2. Thnx for replying @David, m not using jabber.cz.Here are my configs :
      public static final String NEEO_DOMAIN = "mytest.net";
      public static final String NEEO_SERVER_IP = "mytest.pal.com";
      public static final String BASE_URL = "http://mytest.pal.com";

      Delete
  5. Hi @David nice post , i am using this method and able to do group chat but when ever my presence change to offline user automatically leave the chat room. How can i resolve this issue to have a stable group chat.

    thanks in advance

    ReplyDelete
    Replies
    1. Hi Saurabh, Can't solve your issue.
      Try using other real time messaging options like:
      SocketIO - http://socket.io/
      Faye socket android - https://github.com/saulpower/Android-Faye-Client

      Delete
  6. how to check room is deleted by admin ???

    ReplyDelete
  7. Only Owner of the Room can delete the room.

    ReplyDelete
    Replies
    1. I am created a room with 3 user and room owner is me then other 3 user join the room with invitation received . After that i am deleted this room then how other 3 user listen the room has been deleted or any packet get for room deleted by owner....

      Delete
  8. I am created a room with 3 user and room owner is me then other 3 user join the room with invitation received . After that i am deleted this room then how other 3 user listen the room has been deleted or any packet get for room deleted by owner....

    ReplyDelete
    Replies
    1. You may send the room a msg that you are going to leave before deleting that room. You have to make any of them a admin/owner otherwise there will no use of that room. For more information, visit this http://web.mit.edu/svalente/lib/smack_3_0_4/javadoc/org/jivesoftware/smackx/muc/MultiUserChat.html

      Delete
  9. You are problem solver to me :-) love the way you describes it.. Thanks a lot !!!!

    ReplyDelete
  10. Hi david

    Message msg = new Message(to, Message.Type.groupchat);
    msg.setBody(msgText);
    mMultiUserChat.sendMessage(msg);

    Can u tell me whome id i need to send in to??
    In Message class there to you had mention and i am sending message to group so i need to pass group name in to?

    ReplyDelete
  11. Hi Pankaj,

    You have to understand the flow:
    Step 3 - You create a user with userName and login to the server
    Step 4 - You set a group chat and connect to THIS SPECIFIC CHAT (mGroupChatName).
    Therefore, any message you will send in step 6 will be sent to this chat.

    Hope that I helped.

    ReplyDelete
    Replies
    1. Thanks i understand that
      Can you brife about offline messages. i am in one group but i am not connected when i get connected it's not receiver other member message?

      Delete
  12. Hey David, Is it a must that I use jabber.cz server? I want to use my own server account.

    ReplyDelete
    Replies
    1. Hi Jon, You can use any jabber server you want.
      jabber.cz was just an example.

      Delete
  13. nice post and visit it
    http://www.javaproficiency.com/2015/04/openfire-server-tutorial.html

    ReplyDelete
  14. Hey David, thanks for startup. I want to know some more How to create room from aSmack client

    ReplyDelete
  15. hi david nice post, can you please tell me how to add other members in group.
    i know muc.invite method but its not working.

    ReplyDelete
  16. HI David, thank you for your post.
    How to receive incoming invitation and join the room.

    ReplyDelete
  17. I'm still facing problems in listening group chat. How can i know i"m added to a new group.I'm using smack4.1.5 sdk and many old methods are deprecated.

    ReplyDelete
  18. hi Devid please tell me about dependency of MultiUserChat it is getting error that , it is not a public so you can not access this out side the package. please help me.

    ReplyDelete
  19. hi Devid please tell me about dependency of MultiUserChat it is getting error that , it is not a public so you can not access this out side the package. please help me.

    ReplyDelete
    Replies
    1. Can't understand what you mean.

      Delete
    2. Hey Ravi, even I got the same issue. Try getting the multichat instance through multichatmanager. Lot of changes in the smack API.

      Delete
    3. thanx Vivek i did this.
      MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);

      Delete
    4. Hi Devid, Your link is very helpful for me , i stuck to implement
      PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);
      that is giving error can't access the private constructor please help me ..

      Delete
  20. HI David ,i like your post but i am stuck at a point { MessageTypeFilter has private access in org.jivesoftware.smack.MessageTypeFilter} at this line
    PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);please hel[p me to get out of it

    ReplyDelete
  21. HI David ,i like your post but i am stuck at a point { MessageTypeFilter has private access in org.jivesoftware.smack.MessageTypeFilter} at this line
    PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);please hel[p me to get out of it

    ReplyDelete
  22. I'm using smack 4.1 , i got a constructor while use MultiUserChat constructor. There is need to pass three params but In your code just pass to two params.. How..?

    ReplyDelete
  23. After turning wifi off and then on, user cannot send a message to group. How to solve.
    Reply from server: error code="406" type="modify"><not-acceptable

    ReplyDelete
    Replies
    1. PS: I am not actually following this tut,but my issue is related to muc. Thanks.

      Delete
  24. Hi, we are getting the error like 'XMPP error reply received from 'JID': XMPPError: conflict - cancel'

    ReplyDelete