Friday, July 26, 2013

Introduction to XMPP / Jabber






What is Xmpp?

The Extensible Messaging and Presence Protocol (XMPP - originally named Jabber) is an open technology for real-time communication, using the Extensible Markup Language (XML) as the base format for exchanging information. In essence, XMPP provides a way to send small pieces of XML from one entity to another in close to real time.


Xmpp and Jabber, what is the difference?

Xmpp protocol was originally named Jabber. That's all.


ConnectionConfiguration:

Configuration to use while establishing the connection to the server.
Here you can set the Host, Port and Service details:

Google Hangout (talk) configuration for example:

public static final String HOST = "talk.google.com";
public static final int PORT = 5222;
public static final String SERVICE = "gmail.com";

ConnectionConfiguration connectionConfiguration = 
    new ConnectionConfiguration(HOST, PORT, SERVICE);


XmppConnection

Creates a socket connection to a XMPP server.
Gets connectionConfiguration in the constructor.
XmppConnection xmppConnection =
    new XMPPConnection(connectionConfiguration);


Tuesday, July 23, 2013

MultiUserChat with XMPP - Android Tutorial






In this post I will show you how to build an application with multi user chat running on XMPP server.

For a small introduction read this post: Introduction to xmpp
Now let's go fucking coding!
You can download the Jar file from here

Step 1 - Define some parameters

    public static final String HOST = "jabber.cz";
    public static final int PORT = 5222;
    public static final String SERVICE = "jabber.cz";

    String mNickName;
    String mGroupChatName;
    private MultiUserChatmMultiUserChat;
    private XMPPConnectionmXmppConnection;
    private ConnectionConfiguration mConnectionConfiguration;

Step 2 - Create a connection

 mConnectionConfiguration = 
new ConnectionConfiguration(HOST, PORT, SERVICE);
mConnectionConfiguration.setRosterLoadedAtLogin(false);
mXmppConnection = new XMPPConnection(mConnectionConfiguration);
mConnectionConfiguration.setSASLAuthenticationEnabled(true);
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
mXmppConnection.connect();

Step 3 - Login and set status

mXmppConnection.login(USERNAME, PASSWORD, NICK_NAME);
// Set the status to available
Presence presence = new Presence(Presence.Type.available);
mXmppConnection.sendPacket(presence);

Step 4 - Set the Mutliuser Chat

// Create a MultiUserChat using a Connection for a room 
// (room name as the second parameter)
mMultiUserChat = new MultiUserChat(mXmppConnection, mGroupChatName);
mMultiUserChat.create(mNickName);
mMultiUserChat.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
mMultiUserChat.join(mNickName);

Step 5 - Received messages

// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);
mXmppConnection.addPacketListener(new PacketListener()
{
    @Override
    public void processPacket(Packet packet) 
    {
        Message message = (Message) packet;
        if (message.getBody() != null)
        {
            String from = message.getFrom();
            String Body = message.getBody();
            // Add incoming message to the list view or similar
        }
    }
}, filter);

Step 6 - Send message

// import org.jivesoftware.smack.packet.Message;
Message msg = new Message(to, Message.Type.groupchat);
msg.setBody(msgText);
mMultiUserChat.sendMessage(msg);