Connecting to XMPP IM with Smack for Java applications

We will connect to openfire server using smack api . In this tutorial we see smack xmpp connection example . Firstly we will run openfire server if you are new with openfire , then first see how to install openfire . Now we see Smack java client example for connection .

Smack xmpp connection example

We need to add these dependency in pom.xml file or we can add jar file in our project class path.

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 see smack xmpp connection example as below .

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

public class SmackDemo {
public static void main(String[] args) {
SmackDemo smackDemo=new  SmackDemo();

XMPPConnection connection=smackDemo.XMPPConnection();

System.out.println("Connecting to="+connection.getHost()+"  Post:"+connection.getPort());
}

public XMPPConnection XMPPConnection() {
ConnectionConfiguration config = new ConnectionConfiguration(
"host", "port");

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

e.printStackTrace();
}
return connection;
}
}

This smack tutorial is for java xmpp client example . You can build an android chat app from scratch using xmpp and smack with the help of openfire tutorial . In this tutorial we have explained about spark , openfire and smack . Openfire is good java xmpp server . We have seen java xmpp client example in this article . If you are looking for xmpp smack android tutorial  , then this article is for you. In next article we will learn how to login in xmpp server using smack api . You can login in openfire server using spark client also. If you have no idea about spark client ,then read about spark client , where you will learn about spark client installation and how you can connect spark client to openfire .

Leave a Reply

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

15 − 7 =