Showing posts with label xmpp tutorial. Show all posts
Showing posts with label xmpp tutorial. Show all posts

Monday, 7 December 2015

How to Send message from one user to other ( chat example) In xmpp

We have knew how to add roster in xmpp server . Now we will send message from one user to other.
There is simple example for creating chat. There we will send a message using smack api and xmpp server. You can can visit how to install xmpp server.

package com.javaproficiency.demo;

import java.util.Collection;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;



public class SendMessageInChat {

 public static void main(String[] args) {

   AddRoster addRoster = new AddRoster();
 
XMPPConnection connection = addRoster.Connect();
try {
connection.login("yogesh@localhost","123");
System.out.println("Login");

Chat chat = connection.getChatManager().createChat("shivam@localhost", new MessageListener() {

@Override
            public void processMessage(Chat chat, Message message) {
                // Print out any messages we get back to standard out.
                System.out.println("Received message: " + message);
            }

        });
        try {
chat.sendMessage("How are you dear !!");
System.out.println(" Send Message succesfully");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

           connection.disconnect();

} catch (XMPPException e) {

e.printStackTrace();
}
}


private void loginUser(XMPPConnection connection) {
// TODO Auto-generated method stub

}


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 you can see message in xmpp client spark as

How to chat in xmpp server




How to remove Roster or Friend in xmpp server

In last post we have learn how to add roster in xmpp.  Now we will remove roster from roster list of any user. For  first you should  add roster in roster list of user. Now We get roster list of user and compare jabber id of roster and remove it using  removeEntry( RosterEntry ) method  . After remove roster you check roster using this article How to get roster of jabber id in xmpp server .

package com.javaproficiency.demo;

import java.util.Collection;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;



public class RemoveRoster {

  public static void main(String[] args) {

    AddRoster addRoster = new AddRoster();
 
XMPPConnection connection = addRoster.Connect();
try {
connection.login("yogesh@localhost","123");
System.out.println("Login");
Roster roster = connection.getRoster();
String removeJID = "shivam@localhost";
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
          // remove roster whith removeJID
if (removeJID.equals(entry.getUser())) {
roster.removeEntry(entry);
}
}

           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;
   }



}



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








Saturday, 5 December 2015

How To Install Openfire XMPP Server On Ubuntu

Step 1:

Downlaod Openfire from this link http://www.igniterealtime.org/downloads/
 or you can download openfire from this website http://www.igniterealtime.org

step 2:   Exact tar file

Step 3:  go to bin folder and run this command

          ./openfire start

On your terminal you get such message

openfire/bin$  ./openfire start
testing JVM in /usr ...
Starting openfire


step 4:  Now go to browser and hit url

   localhost:9090

if you use local machine otherwise use ip address of machine. We can see such page and click to continue .

how to install openfire



Step 5:  In this step you will select domain and admin port. By default admin console port 9090 and secure admin port is 9091.Domain is ip address of machine. If you want to run openfire then you can use localhost as domain. Click continue for step step of openfire installation.


openfire instalation





Step 6: In this step of openfire installation you will select database settings. If you donot know about it more i suggest to you click continue .

openfire installation



Step 7 :  In this  step you will select database for openfire. For example if i use mysql database then i fill all fields as requirement . i select mysql database then in database url i fill up database name and mysql hostname and more parameter as my requirements .  There is usename mysql usename and password is mysql password.





Step 8: Click to continue:









Step 9:  In This Step you will step administrator account of openfire. Select password for openfire admin.




Step 10. Click Login to admin console of openfire.






Step 11. Use usename and password for openfire administration , which are you have selected during openfire installation .







Step 12: See Openfire Admin Account as







After that you need to learn this tutorial for openfire devlopment  Openfire Tutorial



Related Posts:

Connecting to XMPP IM with Smack for Java applications

Login to XMPP IM with Smack for Java applications

Get Roster List From XMPP IM with Smack API in Java Application