Ejabberd Client Guide

In this post, we are going to look at a quick guide to build a client for Ejabberd Server. I have used Smack API. Other available APIs can be found here. Here you have links for web based, python, c++ etc client etc.

Please note this post just gives you pointers to move ahead in your development.

Before we start with the example, let us first understand what XMPP protocol is.
XMPP stands for eXtended Messaging and Presence Protocol. It is a open standard, decentralized, secure extensible, flexible and diverse protocol for messaging and presence.

XMPP Message looks like this:


<message
    to='romeo@example.net'
    from='juliet@example.com/balcony'
    type='chat'
    xml:lang='en'>
  <body>Wherefore art thou, Romeo?</body>
</message>

It is based on XML. It enables near real time exchange of structured yet extensible data between any two or more network entities. Originally named Jabber, it was developed by the Jabber team for near real time instant messaging, presence information and contact list maintenance.

Smack is an opensource XMPP client library in java.

Proper steps to create a client using Smack can be found at this the smack website and it is easy!

In case you want to add modify the xml tags to your message, you can extend the abstract class Stanza and override the public CharSequence toXML() method as follows:

import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.util.XmlStringBuilder;

public class XmppMessageExample extends Stanza{

 public static final String ELEMENT = "message";
 public static final String BODY = "body";
 private String message;
 /*
  * In case we want to add our custom tag with custom message, we will use additionalMsg attribute
  */
 private String additionalMsg;
 
 public XmppMessageExample(String message, String to, String from, String additionalMsg){
  this.message = message;
  this.setTo(to);
  this.setFrom(from);
  this.additionalMsg = additionalMsg;
 }
 
 /*
  * 
  * (non-Javadoc)
  * @see org.jivesoftware.smack.packet.Element#toXML()
  * 
  * Here we are building the message
  */
 @Override
 public CharSequence toXML() {

  XmlStringBuilder buf = new XmlStringBuilder();
  buf.halfOpenElement(ELEMENT);
  addCommonAttributes(buf);
  buf.optAttribute("addMsg", additionalMsg);
  buf.rightAngleBracket();
  buf.element("body", message);
  buf.append(getExtensionsXML());
  buf.closeElement(ELEMENT);
  return buf;

 
 }

}

Comments

Popular posts from this blog

Writing your own ejabberd Module

npm ECONNREFUSED error

Conditional Flow - Spring Batch Part 6