What is XMPP — iOS Perspective?

Kiran Jasvanee
4 min readMay 29, 2019

--

This journey is going to go from no XMPP knowledge at all to have a fully functional instant messaging iOS app with this coolest protocol.

XMPP?

Basically, an XMPP is a protocol for exchanging stuff. What kind of stuff? Messages and presences. We all know what messages are, but what about presences? A presence is just a way of sharing a “status”, that’s it. You can be ‘online’, ‘offline’, ‘having lunch’, or whatever you want.

Also, there’s another important word: Extensible meaning it can grow. It has kept IoT and other interests in mind to offer instant messaging protocol and it can grow into multiple fields, for example, IoT (Internet of Things). And last, but not least: every piece of information we are going to exchange under this protocol is going to be XML. I can hear you complaining but… Come on, it’s not that bad!

Why XMPP?

Why XMPP, ‘Why not just REST?’. Well, there is a misconception here. REST is not a protocol, it’s just a way of architecting a networked application; So let’s change the question to something that makes more sense: “Why not just build a custom REST chat application?”. Well, How do I know when someone has sent me a message? For XMPP this is trivial: we have an open connection all the time so, as soon as a message arrives at the server, it will send us the message, We have full-duplex. On the other hand, the only solution with REST is polling. We will need to ask the server for new messages from time to time to see if there is something new for us. That sucks. So, we will have to add a mechanism that allows us to receive the messages as soon as they are created, like SSE or WebSockets.

REST uses HTTP, an application level protocol that is built on top of a transport level protocol: TCP. Every time you want to use your REST solution, you will need HTTP. Besides, we have XMPP built on top of TCP that’s going to be always available.

What XMPP offers?

Well, Let’s don’t go so deep into that and just go through what it offers.

  • XMPP is built on top of TCP. It keeps an open connection all the time.
  • Client/Server architecture. Messages always go through a server.
  • Everything we send and receive is going to be XML and it’s called Stanza.
  • We have three different types of stanzas: iq, message, and presence.
  • Every individual on the XMPP network is univocally identified by a JID (Jabber ID).
  • All the stanzas are contained in a Stream. Let’s imagine the Stream as a river where you and the server keep the stanzas.
  • Stream, iq, message, and presence are the core of XMPP. You can find everything perfectly detailed in RFC6120
  • XMPP can be extended to accomplish different stuff. Each extension is called XEP (XMPP Extension Protocol).

What’s a JID?

Jabber ID (JID) is how we univocally identify each individual in XMPP. It is the address to where we are going to send our stanzas.

This is how a JID looks like:

user1@chatapp-server/iPhone

user1 is your username. chatapp-server will be the hostname (domain part) where you will be sending messages & presence. iPhone is your resource part, this is optional, it will be the device you logged in, so you can use user1@chatapp-server from iPhone, Android or Mac…

What’s a Stream?

What’s a Unix socket? From Wikipedia: A socket is a special file used for inter-process communication. These allow communication between two processes. So a socket is a file that can be written by two processes (in the same computer or in different computers in the same network). So the client is going to write to this file and server too.

Ok, but how is a socket related to a Stream? Well, we are going to be connected to a server using a socket, therefore we are going to have a ‘shared file’ between the client and the server. This shared file is like a river where we are going to start adding our XML stanzas (iq, presence & messages). The first thing we are going to write to this file is an opening <stream> tag! And there you go… that’s our stream.

Thoughts? Feedback? Disagreements? Twitter me: @kiranjasvanee, Skype me: kiranjasvanee

If you enjoyed reading this post, please share and clap so others can find it 👏👏👏 !!!!

You can follow me on Medium for fresh articles.

--

--

Kiran Jasvanee

Apple (iOS, watchOS, macOS & tvOS) developer. Creative engineering and data processing boosts me.