Types of Stanzas
Prolongation of the previous Blog Post are the different stanzas described in detail:
1.Presence Stanza ( <presence/>)
The presence stanza, as described by the name, shows entities presence. In simple words, this stanza shows whether the person we want to chat with is online or offline. As per the RFC ( xmpp.org, rfc3920), it is described as an “element that can be seen as a basic broadcast or “publish-subscribe” mechanism, whereby multiple entities receive information about an entity to which they have subscribed ”.
“Head Scratcher”: What is a subscription??
In order to receive, presence information (whether the user is online or not ) about other users, a user must have the presence subscription of the other users. So, if user1 wants to receive presence information about user2 and user3, then user1 must have a presence subscription for user2 and user3. Subscriptions are established using <presence> stanzas.
As an example if user1 wants to subscribe to user2, user1 will send the following presence stanza:
And user2 will accept it using,
Moving onto the explanation of how the <presence> stanza works:
Lets assume there are 3 users with the following JIDs, firstname.lastname@example.org , email@example.com, firstname.lastname@example.org
First let’s see what a presence stanza looks like!! The simplest form of a presence stanza looks like this- <presence/>
If a client sends this stanza to the server, the server publishes the users availability to all the subscribers of that client ( “publish-subscribe” mechanism). So if email@example.com, sends a <presence/> stanza to the server, all the users who are subscribed to firstname.lastname@example.org will see userone as online.
Similarly, if email@example.com sends a <presence type=’unavailable’/> stanza to the server, the rest of the subscribed users will find firstname.lastname@example.org as offline. This explains how our IM clients show us the user’s availability.
Also there are times where messages in the chat clients, like “busy”,”away”,”dnd” & many more including the very famous “status” in WhatsApp are seen!! 😀 😀 . Let’s see how we can establish that using these presence stanzas.
The presence stanza, consists of two extra child elements (optional), namely, <status>,<show>.
- The <status> child can contain any human-readable string, like the status that we put up on WhatsApp!!
e.g. <status> XMPP rocks!! </status>
- The <show> child is mainly used to describe the availability of the user. It only contains away , chat , dnd , and xa values
<show> away </show>
So the whole presence stanza when put together, would look something like
<status>XMPP rocks!! </status
2. Message Stanza (<message/>)
This stanza is used to send the data/chat between the users. It looks something
<body>hey,i am learning about XMPP</body>
Petite explanation, the child element <body> consists the actual chat we type and send, the “to” and the “type” attribute which has already been discussed !!
The IQ stanza is mainly used for the purpose of querying about some kind of information. For example: whenever you login to your IM client, ou always see a list of users with whom you can chat, which shows the availability status of the user on either left/right side of your screen in most cases ( this list is called the “roster” ).
Now the question arises -how does the client come to know of your roster list?? For this kind of info, it needs to first query the server which is accomplished using this IQ stanza.
As an example,
When this stanza is received by the server, the server will respond with “email@example.com’s” roster list.
<item jid=’firstname.lastname@example.org’ name=’EFG’/>
<item jid=’hij’@ example.com’ name=’HIJ’/>
Note: When using the IQ stanza, the “id” and the “type” attributes are mandatory.
“type” attribute values:
- get — Is a request for information or requirements.
- set — Provides required data, sets new values, or replaces existing values.
- result — Is a response to a successful get or set request.
- error — An error has occurred regarding processing or delivery of a previously-sent get or set
Coming to the end about stanzas, We now know the basics of stanzas, and are now ready to build our own chat client. Continuation of the blog will describe-
- Setting up of XMPP Server (Ejabbered)
- Building the Chat Client
Related Links for the Blog Post:
XMPP for Dummies- A Beginners Guide to create your own Chat Application
XMPP for Dummies- Part 2- A 4 Step Life Cycle to Create Your Own Chatter!
XMPP for Dummies- Part 3- Stanzas in Detail