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);
where is the project?
ReplyDeleteHi Silvia, I have no time to make a sample project and upload it.
ReplyDeleteTo use the above code, download the SMACK Jar file:
https://code.google.com/p/asmack/downloads/detail?name=asmack-issue15.jar&can=2&q=
Nice article David. Thanks for the elaborated description.
ReplyDeleteThanks Vipin! :)
DeleteThanx David.It's really very helpful.Thank u so much.
ReplyDeleteThanks dheeraj! :)
DeleteThank You So Much Bro :)
ReplyDeleteThanks Aadi! :)
DeleteHi @David, i followed the same process u mentioned, but m stuck. Here is what m getting :
ReplyDelete11-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)
Maybe the "jabber.cz" server is down, try to find a new server with the same functionality
DeleteThnx for replying @David, m not using jabber.cz.Here are my configs :
Deletepublic 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";
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.
ReplyDeletethanks in advance
Hi Saurabh, Can't solve your issue.
DeleteTry using other real time messaging options like:
SocketIO - http://socket.io/
Faye socket android - https://github.com/saulpower/Android-Faye-Client
how to check room is deleted by admin ???
ReplyDeleteOnly Owner of the Room can delete the room.
ReplyDeleteI 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....
DeleteI 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....
ReplyDeleteYou 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
DeleteYou are problem solver to me :-) love the way you describes it.. Thanks a lot !!!!
ReplyDeleteAnytime :)
DeleteHi david
ReplyDeleteMessage 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?
Hi Pankaj,
ReplyDeleteYou 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.
Thanks i understand that
DeleteCan 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?
Hey David, Is it a must that I use jabber.cz server? I want to use my own server account.
ReplyDeleteHi Jon, You can use any jabber server you want.
Deletejabber.cz was just an example.
nice post and visit it
ReplyDeletehttp://www.javaproficiency.com/2015/04/openfire-server-tutorial.html
Hey David, thanks for startup. I want to know some more How to create room from aSmack client
ReplyDeletehi david nice post, can you please tell me how to add other members in group.
ReplyDeletei know muc.invite method but its not working.
NICKNAME What I can pass
ReplyDeleteWhat do you mean?
DeleteHI David, thank you for your post.
ReplyDeleteHow to receive incoming invitation and join the room.
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.
ReplyDeletehi 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.
ReplyDeletehi 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.
ReplyDeleteCan't understand what you mean.
DeleteHey Ravi, even I got the same issue. Try getting the multichat instance through multichatmanager. Lot of changes in the smack API.
Deletethanx Vivek i did this.
DeleteMultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);
Hi Devid, Your link is very helpful for me , i stuck to implement
DeletePacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);
that is giving error can't access the private constructor please help me ..
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
ReplyDeletePacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);please hel[p me to get out of it
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
ReplyDeletePacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);please hel[p me to get out of it
can anyone share demo code ??
ReplyDeleteI'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..?
ReplyDeleteAfter turning wifi off and then on, user cannot send a message to group. How to solve.
ReplyDeleteReply from server: error code="406" type="modify"><not-acceptable
PS: I am not actually following this tut,but my issue is related to muc. Thanks.
DeleteHi, we are getting the error like 'XMPP error reply received from 'JID': XMPPError: conflict - cancel'
ReplyDelete