Implementing XMPP – Xtensible Messaging and Presence Protocol – Part 2 – Setup android client

logo-openfire copy_2

Hello guys, I you have landed here directly and yet to setup your server, i would recommend you to go the previous part of this series that contain the server setup.

If you have already setup the server, you can dive in this post.

First create a new project from File -> New -> New Project. Enter the project name as per your requirement.

Now we will add some dependency to setup the client side. Paste below lines in your app level build.gradle file and hit sync.

implementation "org.igniterealtime.smack:smack-tcp:4.2.4"
implementation "org.igniterealtime.smack:smack-experimental:4.2.4"
implementation "org.igniterealtime.smack:smack-android:4.2.4"

Also add below code to support your app on pre-lollipop version and sync your project.

configurations {
    all*.exclude group: 'xpp3', module: 'xpp3'
}

Now first we will create a method for creating the connection to our server. Lets call it connect(). Add code shown below in the connect method.

public void connect() throws IOException, XMPPException, SmackException {
    XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration.builder()
            .setXmppDomain("domain name")
            .setHost("address of your server")
            .setResource("Mobile")
            .setPort(5222)
            .setKeystoreType(null)
            .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
            .setCompressionEnabled(true).build();   
    mConnection = new XMPPTCPConnection(conf);
    mConnection.addConnectionListener(mOnConnectionListener);
    try {
        mConnection.connect();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

Now, let’s see what we have done in above code. So in order to connect to the server, we first need to set the configuration of the client. In configuration, we need to provide following,

  • Domain name – Name of the XMPP domain, which we saved in last article.
  • Server address – Address of your server, in case of local host, you have to provide the ip address of the system, on which the openfire is running.
  • Resource – It is the name of the resource  from where the connection is made.
  • Port – Here we are setting it to  5222.
  • KeyStoreType – Here we are setting it null to avoid the issue of connection failure with SSL.
  • SecurityMode – For this demo setup we are setting it up as disable. I would recommend setting it to enable in production mode.

After providing all this configuration to the XMPPTCPConnectionConfiguration, you require it to build the object of it.

This XMPPTCPConnectionConfiguration object will be provide to the XMPPTCPConnection for further connection.

Now we will add the connection listener to the XMPPTCPConnection object, to receive the events obtained from the connection.

Add below code in your class, to receive the events.

private ConnectionListener mOnConnectionListener = new ConnectionListener() {
    @Override
    public void connected(XMPPConnection connection) {
        Log.d(TAG, "Connected Successfully");
    }

    @Override
    public void authenticated(XMPPConnection connection, boolean resumed) {
        Log.d(TAG, "Authenticated Successfully");
    }

    @Override
    public void connectionClosed() {
        Log.d(TAG, "Connectionclosed()");
    }

    @Override
    public void connectionClosedOnError(Exception e) {
        Log.d(TAG, "ConnectionClosedOnError, error " + e.toString());
    }

    @Override
    public void reconnectionSuccessful() {
        Log.d(TAG, "ReconnectionSuccessful()");
    }

    @Override
    public void reconnectingIn(int seconds) {
        Log.d(TAG, "ReconnectingIn() ");
    }

    @Override
    public void reconnectionFailed(Exception e) {
        Log.d(TAG, "ReconnectionFailed()");
    }
};

In the above code snippet, method name itself represents their work so i m not going in detail with it.

Now to create the connection to the server, we will call,

mConnection.connect();

When the client is connected to the server successfully, we will receive its event in connected().

Now as all the configuration is ready you can call it from whereever you want. You can create connection on button click or when activity is launched or you can even connect it from service.

Thats is all you need to know about creating the connection of client with the server.

Thanks for your time. Let me know if you have any query in this article.

Happy coding!

Advertisements

One thought on “Implementing XMPP – Xtensible Messaging and Presence Protocol – Part 2 – Setup android client

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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