Thursday, 8 September 2011

XMPP or Jabber for Chatting in Android

This Tutorial will tell you how to create a XMPP Client  for Android.
You can connect to any Server  like Gmail or Facebook or create your own Server using Open Fire.

  • For Information regarding how to setup Jabber Messaging Server and Use Miranda IM Client refer to:

  • And download Miranda IM Client 
  • Next thing is to Connect to this Server Using your own Android Application: 
          You need to use the Smack Api for this purpose, which can be downloaded from the follwing link:
After You have downlaoded the Smack Library Now you need to add it in your project's Refrenced Libraries. 
       1.Connection With The Server:
private XMPPClient xmppClient;
    private XMPPConnection connection;
    private ArrayList<String> messages = new ArrayList(); 
    public void login(String userName, String password) throws XMPPException
    ConnectionConfiguration config = new ConnectionConfiguration("HOST",PORT, "SERVICE");
    connection = new XMPPConnection(config);
    connection.login(userName, password);
  2.Send Message to a Person:
   Message msg = new Message("Recepiont",;

  3.For incoming messages from other users:
  public void setConnection
 (XMPPConnection connection) {
 this.connection = connection;
 if (connection != null) {
   // Add a packet listener to get messages sent to us
  PacketFilter filter = new MessageTypeFilter(;
  connection.addPacketListener(new PacketListener() {
   public void processPacket(Packet packet) {
    Message message = (Message) packet;
     if (message.getBody() != null) {
    String fromName = StringUtils.parseBareAddress(message.getFrom());
    Log.i("XMPPClient", "Got text [" + message.getBody() + "] from [" + fromName + "]");
    messages.add(fromName + ":");
    // Add the incoming message to the list view Runnable() {
    public void run() {
   }, filter);
  4.To Sign Out or Disconnect from the Server: 

 public void signOut()
  There are a lot of other things which you can do using smack API.
  You can find the detailed documentation of each class in the following link:




  1. welcome iintrance..For any other queries please feel free to communicate

  2. Hi Karan,
    Iam faing problem in add contact ,I am using xmpp+smack api.
    I am sending request to contact and after recieving packet ,i want to accept new request on a button click but no idea how to manage it after recieving packet .

  3. Try the following code:

    * New Friend can be Added using
    * the Roster Class
    public void addFriend()
    Roster roster=connection.getRoster();
    try {

    } catch (XMPPException e) {
    // TODO Auto-generated catch block

  4. Hey Karan..In my application ,there is a chat application only for application users.And before this I never implemented chat .I have no idea how to implement chat client and what are the parts of the server side..So can you advice me about how to do it..or you can give me the tutorial link for basic chat application..Thanx in andvance

  5. Hi Karan, this is helpfull information. iam new to android and asmack(smack for android) api. iam facing problem to get the presence status of friends. ofcourse asmack defines corresponding classes to do this but does asmack support this feature. if possible please explain me with source code. thank you

  6. Does the smack api works on Android ? I'm using it and i can't connect to Openfire server and the error i'm getting seems to have to do with the fact that smack doesn't work with android and i have to use aSmack which is a third part version of smack to use with android

  7. Hi Karan,

    Can you help me with XMPP as it is totally new for me and i want to know how to start with basic chat application in android.

  8. I want to thank you for writing this article.This is great Article for me. It also more very informative & awesome. I expect more articles from you in future.


iOS Tutorial on Enumeration  So as we are talking about enumeration in iOS,the first thing comes into mind are Collections such as Array...