powered by Jive Software

Cannot receive/send message using Smack!

Hey all, I am a new user, I have a fresh and clean install of Linux Ubuntu 12.04 (LTS) and I am trying to create a small program that can contact with Spark IM, using an Openfire local server.

To achieve this purpose I have followed these tutorials:

  1. Installing Openfire with a very basic setup: http://www.javacodegeeks.com/2010/08/openfire-server-installation.html

  2. Installing Spark and creating testusers in the Openfiser local server: http://www.javacodegeeks.com/2010/09/openfire-server-configuration.html

  3. Creating a Java application that can communicate with the Spark IM client using the Smack API: http://www.javacodegeeks.com/2010/09/xmpp-im-with-smack-for-java.html

Everything is fairly simple. The Openfire local server works like a charm, and so does the Spark IM client. However, the Java Code using the Smack API is a complete failure. The only thing that works, is the status and the status message. Sending and receiving messages does not work at all.

Following are the two Java classes I use, the XmppManager and then the XmppTest:


import java.util.Collection; 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.RosterEntry;
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 " +  server + ", port " + port));         SmackConfiguration.setPacketReplyTimeout(packetReplyTimeout);         config = new ConnectionConfiguration(server, port);
        config.setSecurityMode(SecurityMode.disabled);         connection = new XMPPConnection(config);
        connection.connect();         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);         presence.setStatus(status);
        connection.sendPacket(presence);     }     public void destroy() {
        if (connection!=null && connection.isConnected()) {
    }     public void sendMessage(String message, String buddyJID) throws XMPPException {
        System.out.println(String.format("Sending message " + message + " to user " + 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 " + user + " with name " + name));
        Roster roster = connection.getRoster();
        roster.createEntry(user, name, null);
    }     public void printRoster() throws Exception {
        Roster roster = connection.getRoster();
        Collection<RosterEntry> entries = roster.getEntries();          for (RosterEntry entry : entries) {
            System.out.println(String.format("Buddy:" + entry.getName() + " - Status:" + entry.getStatus()));
    }     private class MyMessageListener implements MessageListener {         @Override
        public void processMessage(Chat chat, Message message) {
            String from = message.getFrom();
            String body = message.getBody();
            System.out.println(String.format("Received message " + body + " from " + from));
        }     } }


public class XmppTest {
        public static void main(String[] args) throws Exception {
                String username = "admin";
        String password = "admin";
                XmppManager xmppManager = new XmppManager("localhost", 5222);
        xmppManager.performLogin(username, password);
        xmppManager.setStatus(true, "Hello everyone");
                String buddyJID = "user1";
        String buddyName = "user1";
        xmppManager.createEntry(buddyJID, buddyName);
                xmppManager.sendMessage("Hello mate1", "user1@ubuntu-virtualbox.com");
        xmppManager.sendMessage("Hello mate2", "user1@ubuntu-virtualbox.com");
        xmppManager.sendMessage("Hello mate3", "user1@ubuntu-virtualbox.com");
                boolean isRunning = true;
                while (isRunning) {
            } }

My Openfire local server has two users:

admin -> with admin privileges

user1 -> a regular guy

I am trying to make the “admin” user communicate with “user1”, however that seems to be impossible.

Furthermore, when I run the XmppTest class, this is what I get:

Initializing connection to server localhost, port 5222
Connected: true
Creating entry for buddy user1 with name user1
Sending message Hello mate1 to user user1@ubuntu-virtualbox.com
Sending message Hello mate2 to user user1@ubuntu-virtualbox.com
Sending message Hello mate3 to user user1@ubuntu-virtualbox.com
Buddy:user1@ubuntu-virtualbox.com - Status:subscribe
Buddy:user1 - Status:subscribe
Buddy:user1 - Status:null

Which is weird, because my “admin” account knows about “user1” and has it on the friends list. Furthermore, on the Spark IM client, “user1” does not receive anything from the admin, even thouh in the code I send 3 messages to “user1”.

Also, in the Spark client, when I type something to admin, nothing is printed in the Eclipse console. I do seem to get a “null” print from time to time due to reasons that seem to be random and beyond me.

What am I doing wrong? Is the code sample I use outdated ? I would really appreciate some help :S

Got it. After a week I finally discovered that this is in fact a bug from Openfire. The workaround is simple and can be found here:

http://stackoverflow.com/questions/21287847/cannot-receive-send-message-using-sm ack

We have no problem with using a “.com” domain in Openfire. And our JIDs are then "node@domainname.com".

It works for us.

Did you make sure that your Openfire domain is indeed “ubuntu-virtualbox.com” and not “ubuntu-virtualbox”?

(You see it on Openfire’s startpage or in the property xmpp.domain)

It’s definetly atypical that you need to remove the TLD. I guess it’s some quirk in your configruation.

I would guess that his XMPP domain in Openfire doesn’t contain the *.com *and the messages are simply being addressed to a non existant XMPP domain.

I will be pretty shocked if such a bug exists in Openfire.