that were shone when I got tempered!

Write XMPP client using Smack API

leave a comment »

Smack is written in Java. So, it is cross platform. Current version can be downloaded from here.

To start with this, create a Java Project (eg : XMPPProject) and, copy the “smack.jar” and “smackx.jar” files into your classpath.

These are two main classes for this application.

package com.javacodegeeks.xmpp;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;

public class XmppManager {

private static final int packetReplyTimeout = 500; // millis

private String server;
private int port;

private ConnectionConfiguration config;
private XMPPConnection connection;

private ChatManager chatManager;
private MessageListener messageListener;

public XmppManager(String server, int port) {
this.server = server;
this.port = port;

public void init() throws XMPPException {

System.out.println(String.format("Initializing connection to server %1$s port %2$d", server, port));


config = new ConnectionConfiguration(server, port);

connection = new XMPPConnection(config);

System.out.println("Connected: " + connection.isConnected());

chatManager = connection.getChatManager();
messageListener = new MyMessageListener();


public void performLogin(String username, String password) throws XMPPException {
if (connection!=null && connection.isConnected()) {
connection.login(username, password);

public void setStatus(boolean available, String status) {

Presence.Type type = available? Type.available: Type.unavailable;
Presence presence = new Presence(type);



public void destroy() {
if (connection!=null && connection.isConnected()) {

public void sendMessage(String message, String buddyJID) throws XMPPException {
System.out.println(String.format("Sending mesage '%1$s' to user %2$s", message, buddyJID));
Chat chat = chatManager.createChat(buddyJID, messageListener);

public void createEntry(String user, String name) throws Exception {
System.out.println(String.format("Creating entry for buddy '%1$s' with name %2$s", user, name));
Roster roster = connection.getRoster();
roster.createEntry(user, name, null);

class MyMessageListener implements MessageListener {

public void processMessage(Chat chat, Message message) {
String from = message.getFrom();
String body = message.getBody();
System.out.println(String.format("Received message '%1$s' from %2$s", body, from));



package com.javacodegeeks.xmpp;

public class XmppTest {

public static void main(String[] args) throws Exception {

String username = "testuser1";
String password = "testuser1pass";

XmppManager xmppManager = new XmppManager("myserver", 5222);

xmppManager.performLogin(username, password);
xmppManager.setStatus(true, "Hello everyone");

String buddyJID = "testuser2";
String buddyName = "testuser2";
xmppManager.createEntry(buddyJID, buddyName);

xmppManager.sendMessage("Hello mate", "testuser2@myserver");

boolean isRunning = true;

while (isRunning) {





Description :
XMPPConnection – to Connect

This is basically to connect to the remote server. Connecting and performing the Login can be done as follows :

// Create a connection to the XMPP server.
XMPPConnection connection = new XMPPConnection("");
// Connect to the server
// Most servers require you to login before performing other tasks.
connection.login("myuser", "mypass");


ConnectionConfiguration – to configure sohisticated conection attributes

This can be used to achieve more sophisticated connection attributes. In this case, various parameters can be configured (server host, port and securrity -encrypt- issues). You can use SmackConfiguration class to configure an internal Smack stack.

ConnectionConfiguration config = new ConnectionConfiguration(serverHost, serverPort);
XMPPConnection connection = new XMPPConnection(config);
connection.login("myuser", "mypass");


ChatManager – to manages chats
From a valid XMPPConnection, you can recive a ChatManager object. This keeps a referenc to all current chats and it allows you to create chat instances (series of messages sent between two users). Chat objects can send messages to the other participants. These messages can be a plain String or construted XMPP messages represented by the Message class.


Message class – messages
Message class provides the direct access to all message attributes (eg : message types).


MessageListener – to listen
To process incoming messages, you must implement the MessageListener and attach it to the chat.

ChatManager chatManager = connection.getChatManager();
Chat chat = chatManager.createChat("mybuddy", new MyMessageListener());


Presence – to setup the status
You can set your online status using the Presence class.

Presence presence = new Presence(Presence.Type.available);
presence.setStatus("What's up everyone?");


Roster – allocate user(s)
A new buddy can be allowcated to your list by using the Roasterr class. A user’s Roaster is a collection of users a persion recieves presence updates for. A user can be associaated with a group too.

Roster roster = connection.getRoster();
roster.createEntry(user, name, null);


Note :
In this example no security authentication is done (it is disabled). So, there’s no any protection on the credentials and the messages and the messages that has been exchanged.
Here, an infinite loop has been created (via isRunning boolean) to send the message from the other client and see the response from the console. In real time situaations, this must be set as false if the program exit. And the thread must be slept for some time otherwise machine’s CPU will be much higher.

You can find the project in here.

Related Articles :


Written by Namal Fernando

December 18, 2015 at 6:44 am

Posted in Smack API

Tagged with , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Ruth's Reflections

Contemplations from quakey quirky Christchurch

TED Blog

The TED Blog shares interesting news about TED, TED Talks video, the TED Prize and more.


Learn and discover simple things

Meihta Dwiguna Saputra's Knowledge Base

~In learning you will teach and in teaching you will (re)learn~

The Java Blog

Thoughts, tips and tricks about the Java programming language


that were shone when I got tempered!

%d bloggers like this: