Courtesy: orbtr.net

Flash Back

From the previous post we got a faint idea of how Instant Messaging works, right? But even then, let me just give you a flash back:

  1. IM (Instant Messaging) follows a certain protocol , ie. XMPP
  2. XMPP stands for Extensible Messaging and Presence Protocol
  3. XMPP is a protocol that transports small amounts of data between two network endpoints.
  4. The data exchanged is structured in XML.
  5. Data is exchanged via Streams established between the server and client or another server.
  • The data being exchanged is called as a Stanza.
  • There are 3 types of stanzas:

1. Presence – This provides information related to a user’s presence (online,offline,status messages etc).

2. Message – This is the actual messages sent between users.

3. IQ – This provides information based on queries , such as to retrieve a users information or update the users profile, get a user’s roster information etc.

 

The 4 Step Life Cycle of an XMPP Connection

So now that we have had a small revision, lets move on to the next part. As discussed earlier we know that stanzas need to be exchanged between a server, client or any other server for the XMPP functionality to actually work. We also know that in order to exchange the stanzas, we need to first establish streams between two servers or the server/client. So lets first understand that how streams or a connection between servers or client/server is established.

We can break this life cycle into four parts:

a) Connection

In order to for streams to established, the server client must first establish a connection. Let us take an example for this:

When we type an address into our browsers address bar and hit enter, our browser searches for an ip address to download the web page from. An entry of our websites name (like: www.example.com) and a corresponding ip address (lets say, 124.124.124) is stored as a record in a DNS server. So the browser requests the DNS server to give the ip address for www.example.com and then goes to the specified ip address (124.124.124) and downloads the page to be presented to us.

Similarly, in the case of  XMPP , SRV records are used. When a client wants to connect to a server or to another server, it first queries for the appropriate SRV record at the server’s domain. Once a record is retrieved, a connection is established.

b) Creating Streams

Now that a connection has been established, we need to open streams from both sides, in order to allow the exchange of elements, i.e Stanzas. This is done , by sending a <stream:stream> to the server. The server then responds back with a <stream:stream> tag.

Once this has been done, we now have streams opened in both directions. Now stanzas can be exchanged between the two endpoints.

Another point to be noted, along with the <stream:stream> tag , <stream:features> tag is also sent by the server (this happens only once, when the streams are being established). This tag embodies the features of the stream established with the server .These features are mostly related to the encryption and authentication options supported by the stream, for example whether the stream uses TLS or not.

c) Authentication

Now that we have established a connection and the streams have been setup, the next step is authentication. XMPP authenticates clients using Simple Authentication and Security Layers (SASL) protocol [Check out the Details here], though many other authentication mechanisms can be used.

Once the client has been authenticated, the client is bound to a resource.

Now you might  wonder what is a resource? Consider you login to your account,from your workplace using  an IM client( say for example PIDGIN ). Now you do not logout from it, go home and using the same  credentials, you can login again with the same credentials using another client. You do not need credentials from a different account to do this. In a nut shell, multiple logins is made possible using resources. Every time you login from another client, each of them is assigned a resource (more like an identifier). This is provided either by the server or the client itself.

Once authenticated and assigned a resource, you can start exchanging your Stanzas.

d) Disconnection:

Once your done, you decide to logout. When you click logout on your client, what your client does is, it sends a presence stanza to your server (link to the previous blog for stanzas) telling the server that you are unavailable. So the other users, who have you in their rosters will come to know that you are offline. After this the stream is terminated using </stream:stream>

( Note: We opened the stream earlier using <stream:stream>)

So this is the entire Life Cycle of how we actually start chatting with our friends/colleagues. Doesn’t sound all that difficult now right?

Well, in our next bog, we will concentrate more on the building blocks of the XMPP protocol, the “Stanzas”, in detail. This will help us better understand, how your client( eg WatsApp,GTalk)  comes to know, whether the user you want to chat with is online or not, whether the user is currently typing or not, how you receive the messages etc. So till then feel free to reach out to me! Social details are down below or you can comment your likes, dislikes or questions. Will be happy to revert back!

 

Some reads on the Cloud & Related technology you might find interesting:

1. The Abstract Design Pattern with Examples – Solving the Design Pattern Conundrum
2. Hadoop Gives way to Real Time Big Data Stream Processing – The 3 Key Attributes – Collect | Process | Analyze
3. MongoDB on the Clouds – An Introduction to an Evolution from RDBMS
4. Simplifying Coding with the Decorator Design Pattern – When To & When Not to Use it!