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 .

Smack java client example

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

Roster roster = connection.getRoster();
// yogesh send roster request to shivam

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

} catch (XMPPException e) {


public XMPPConnection Connect() {

ConnectionConfiguration config = new ConnectionConfiguration(
"localhost", 5222);

* ConnectionConfiguration config = new ConnectionConfiguration(
* "", 5222);
XMPPConnection connection = new XMPPConnection(config);
try {
} catch (XMPPException e) {

return connection;

After execution  of this code you can see this output :
 Login send roster request And
how to add roster in xmpp server
In this article we have discuss about how to send roster request in xmpp . If you are looking openfire and smack tutorial , then visit openfire tutorial with spark and smack api.


Leave a Reply

Your email address will not be published. Required fields are marked *

59 + = 63