Blazeclan Technologies Recognized as a Niche Player in the 2023 Gartner® Magic Quadrant™ for Public Cloud IT Transformation Services

XMPP Tutorial for Dummies – Creating Your Own Chat Application from Scratch – Part 1

  • Cloud
  • By Team Blazeclan

We all use Gmail (Gtalk, now hangouts), FaceBook, SKYPE, Yahoo messenger, WatsApp and various other messaging tools on a daily basis. Many of us even use these IM clients  within our organisations for our official purposes. Even the notifications we receive on our android cells (GCM), make use of the same underlying technology.  So today just out of curiosity, I decided to take a step into this world of, ‘InstantMesagging (IM)’ and understand how this technology works to provide us with such an excellent way of communication.

So, here I present to you all, XMPP(eXtensible Messaging and Presence Protocol)! The backbone of today’s Instant Messaging world. [Google moving away from XMPP though].

So in this blog I will provide you all with an overview of XMPP. And in the future blogs series, we will learn how to implement XMPP based server (ejabberd) and build our own XMPP chat application.

 

So Let’s Start from the Basics!

What is XMPP? & How to use it?

Extensible Messaging and Presence Protocol (XMPP) is a protocol for streaming Extensible Markup Language (XML) elements in order to exchange structured data in close to real time between any two network endpoints.” Check out the official RFC for this Protocol.

In simple words, XMPP can be considered as a protocol for transporting small pieces of data between two places. These pieces of data are in a structured format ( XML ).

XMPP provides us with systems for building:

1. Publish-subscribe services

2. Multi-user chat

3. Form retrieval and processing

4. Service discovery

5. Real-time data transfer

6.Privacy control

7. Remote procedure calls

 

Stanzas – The Building Blocks

Lets take a look at the structure XMPP uses. XMPP mainly consists of streams. Structured data is sent between the  network endpoints using these streams.  These streams are established both ways( between the server and the client.). So for a single connection between a server and a client, two streams are established The structures data that I am referring to in the above sentences, in the  XMPP language are referred to as Stanzas. All the communication features, like whether a user is online or not, chatting, group chats, a user’s status (available, away, for eg. your status on WhatsApp)  that XMPP servers provide are through the exchange of these Stanzas.

 Stanzas are of three types:

1. Presence

2. Message

3. IQ

 

Is the Other Guy Online? The Presence Stanza Tells You:

The <presence> stanza controls and reports the availability of an entity (can be a client, another server etc). For eg: When you use your Gtalk client, you come to know whether the person you want to chat with is online or not. This information is provided by this stanza. We also create various groups and chat, this feature is also taken care of by this stanza.

A simple example of what a presence stanza looks like:

<presence>

<show>away</show>

<status>Coffee Break</status>

</presence>

 The above presence stanza tells us that, the user is “away” with a message “ Coffee Break”

 

The Actual Meat – The Message Stanza:

You might have already guessed, this stanza is used to send messages from one entity to another.

 Example:

<message from=’abc@example.com’

to=’xyz@example.com’

type=’chat’>

<body>We have had a most delightful evening, a most excellent ball.</body>

</message>

The above Message stanza tells us that, the user abc@example.com is sending a message to antoher user, ’xyz@example.com’ . Here, ’xyz@example.com’ and ‘abc@example.com‘ are referred to as the JIDs (Jabber Identifiers). These are the addresses. Each user, registered with a Jabber (XMPP) server will be provided with a unique JID, similar to our email IDs (example@gmail.com)

 

Those Xtra Details – The IQ Stanza Cover them:

The <iq> stanza stands for Info/Query. It is mostly used for the purpose of querying the server for certain information. For example, the list of users of a particular chat group. It provides a request and response mechanism for XMPP communication.

 Example:

<iq from=’exmaple@gmail.com’ type=’get’ id=’someid’>

<query xmlns=’jabber:iq:roster’/>

</iq>

 This IQ stanza here, is asking the server to respond with the example@gmail.com‘s roster.

 

What’s To Come Next ?

Don’t worry if u did not quite get the hang of Stanzas!!! Stanzas will be covered in detail, in the upcoming blogs. Only with the help of these three types of stanzas, combined together in different ways, can provide us with the implementation of many sophisticated features. That’s all for now guys!!! In the upcoming blogs, the topics to be covered will be:

  1. The connection life cycle
  2. Stanzas in detail
  3. Building our own chat client ( using ejabberd, strophe.js  and PHP )

Don’t Miss a Single Blog! Subscribe to our blogs to keep up to date to the latest technologies & Cloud!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.