Login to XMPP IM with Smack for Java applications

In previous article we have seensmack xmpp connection example , In this smack tutorial we will learned how to login xmpp server using smack api. This smack java client example show you that first we will connect to xmpp server then we will login using username and password .

Smack XMPP Java Client Login Example

First we will add smack maven dependencies in our pom.xml or add corresponding jar file in java project.

Maven dependency

<dependency>
<groupId>jivesoftware</groupId>
<artifactId>smack</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>jivesoftware</groupId>
<artifactId>smackx</artifactId>
<version>3.1.0</version>
</dependency>

Now we will write java xmpp client example for login into xmpp server . 

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

public class LoginSmack {
public static void main(String[] args) {
LoginSmack loginSmack=new LoginSmack();
XMPPConnection connection=loginSmack.Connect();

try {
connection.login("Username","Password");
} catch (XMPPException e) {

e.printStackTrace();
}
System.out.println("Login Successfully");
}

/**
*
* @return XMPP Connection
*/

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

}

In this tutorial we have seen how to login in XMPP server by smack api using java. If you are new with XMPP and looking tutorial on XMPP Server , then i will recommended xmpp smack tutorial . These tutorial also helpful for , who looking xmpp smack-android tutorial . In these tutorial you will learn about openfire , spark and smack java.

Leave a Reply

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

92 − 87 =