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

           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 :

send roster request


how to add roster in xmpp server


  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


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

    SEO Company in Chennai