share

Monday, 7 December 2015

how to add roster in xmpp using smack api

In last article we have learn how to login on xmpp server using smack api . Now In this article we will learn how to add friends( roster ) in buddy using smack api.For add roster in xmpp first you should make two account. See It how to resister on xmpp server if you do not know how to register user on xmpp server. after that you can send roster request using below code . 


package com.javaproficiency.demo;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.Roster.SubscriptionMode;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

public class AddRoster {

 public static void main(String[] args) {

   AddRoster addRoster = new AddRoster();
 
XMPPConnection connection = addRoster.Connect();
try {

                     // user yogesh login on xmpp server
connection.login("yogesh@localhost","123");
System.out.println("Login");

           Roster roster = connection.getRoster();
           // yogesh send roster request to shivam
           roster.createEntry("shivam@localhost","myking",null);

           System.out.println(" send roster request");
           connection.disconnect();

} catch (XMPPException e) {

e.printStackTrace();
}
}


public XMPPConnection Connect() {
 
  ConnectionConfiguration config = new ConnectionConfiguration(
  "localhost", 5222);

  /*
  * ConnectionConfiguration config = new ConnectionConfiguration(
  * "192.163.2.200", 5222);
  */
  XMPPConnection connection = new XMPPConnection(config);
  try {
  connection.connect();
  } catch (XMPPException e) {
 
  e.printStackTrace();
  }
  return connection;
  }


}



After execution  of this code you can see this output :

 Login
 
send roster request


And  


how to add roster in xmpp server








2 comments:



  1. I am a regular reader of your blog. the blog is very interesting and will be much useful for us.
    SEO Company in Chennai

    ReplyDelete

  2. Great and useful article. Creating content regularly is very tough. Your points are motivated me to move on.


    SEO Company in Chennai

    ReplyDelete