Minimal working example

Hi,
I had a minimal working IM with smac 3.x.x for android
After upgrading to smac 4.4.6 I am getting a lot of issues.
I am using hot-chilli.net server.
The API documents are good but I need a simple minimal working example that works with smac 4.4.6. specifically I need to connect, log in, send and listen to incoming messages. I am trying to just log incoming messages to logcat.
All the tutorial I found online deal with older versions of smac.
if you could point somewhere with a basic sample code that works with 4.4.6 I will greatly appreciate it.

cheers

The following works with Openfire started in ‘demoboot’ mode (which automatically creates the user accounts that are used by this example). You can obviously also replace this with your own users.

Note that this example disables important security features. You should not use this for anything important!

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.filter.MessageWithBodiesFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.MessageBuilder;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;

import java.time.Duration;

public class TestClients
{
    public static void main(String[] args) throws Exception
    {
        //SmackConfiguration.DEBUG = true;

        final XMPPTCPConnectionConfiguration configSender = XMPPTCPConnectionConfiguration.builder()
            .setUsernameAndPassword("john", "secret")
            .setXmppDomain("example.org")
            .setResource("test")
            .setHost("localhost")
            .setPort(5222)
            .addEnabledSaslMechanism("PLAIN")
            .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
            .build();

        final XMPPTCPConnectionConfiguration configReceiver = XMPPTCPConnectionConfiguration.builder()
            .setUsernameAndPassword("jane", "secret")
            .setXmppDomain("example.org")
            .setHost("localhost")
            .setPort(5222)
            .addEnabledSaslMechanism("PLAIN")
            .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
            .build();

        final XMPPTCPConnection connectionSender = new XMPPTCPConnection(configSender);
        final XMPPTCPConnection connectionReceiver = new XMPPTCPConnection(configReceiver);

        try
        {
            connectionSender.setUseStreamManagement(false); // To reduce output in the debug log.
            connectionReceiver.setUseStreamManagement(false); // To reduce output in the debug log.

            connectionSender.connect();
            connectionReceiver.connect();

            connectionSender.login();
            connectionReceiver.login();

            Thread.sleep(Duration.ofSeconds(2).toMillis());

            // Listen for incoming messages.
            connectionReceiver.addAsyncStanzaListener((stanza -> System.out.println("Received: " + ((Message) stanza).getBody())), MessageWithBodiesFilter.INSTANCE);

            // Construct and send message.
            final Message message = MessageBuilder.buildMessage()
                .to(connectionReceiver.getUser())
                .setBody("Hello!")
                .build();
            connectionSender.sendStanza(message);

            Thread.sleep(Duration.ofSeconds(2).toMillis());
        }
        finally
        {
            connectionSender.disconnect();
            connectionReceiver.disconnect();
        }
    }
}

Thank you very much. Unfortunately I am not even able to connect anymore.
It worked ok with smack 3.4.0 but I needed the message delivery status functionality so I upgraded to smack 4.4.6. I tried using your example to just connect my android app to hot-chilli server but it times out.

That sounds like a networking issue. Are you sure that you have provided the correct XMPP domain name, and host (alternatively, you can remove the setHost method call and let Smack look up the correct value itself).

Without any kind of debug logging, it’s hard to tell what’s going wrong for you.

Thank you. got it to work again. I had to implement stanza parsing error callback. it was disconnecting because of a lot of unparsable stanzas. logging these stanzas yielded stuff like :“name: hot-chilli Skype transport”, “instagram transport”, “hangout trastport” etc. I have no idea where they come from. A side question: I’m implementing messaging in ways of using the Chat class. I also see code, as yours as well not using a Chat class but rather an asynchronous stanza listener. Are both equivalent? Obviously you can get more data out a stanza but other than that?
thanks again

This topic was automatically closed 62 days after the last reply. New replies are no longer allowed.