How to build a low-latency and highly scalable instant messaging infrastructure?

Narendra Soni
3 min readJan 4, 2024

From my own learning in building applications, if I have to mention one particular technology/protocol to build low-latency, highly scalable, fault-tolerant instant messaging infrastructure, then that has to be XMPP.

WhatsApp has ~2 Billion users; it uses a variation of XMPP for its popular chat service. Reference here.

XMPP stands for Extensible Messaging and Presence Protocol. It is designed for real-time communication, primarily used for instant messaging (IM), presence information, and contact list maintenance. XMPP is an open standard for message-oriented middleware, supporting a wide range of applications beyond instant messaging, including Voice over IP (VoIP), file transfer, and more. XMPP follows a client-server architecture where clients connect to a server, and the server manages the communication between clients. It supports a publish-subscribe model and is designed to be extensible, allowing for the addition of new features and functionalities. Typically, XMPP uses a series of XML-based stanzas for communication. It operates over a persistent TCP connection and often requires additional features for secure communication, like TLS.

There is a wealth of information related to XMPP on the official website here.

As I mentioned above, XMPP is an open standard/protocol. Hence, there are a number of servers, clients developed which are XMPP compliant. The choice is yours which server you choose for your purpose. Some of the popular XMPP servers are Ejabberd, Prosody, and OpenFire.

In this article, I am going to refer to Ejabberd, as it is the most popular XMPP server out there. Organizations like Meta(Prev. Facebook), Riot Games, Telegram, etc., started their projects by taking the open source version of Ejabberd, but over a period of time, they modified it, and now they have their own version of the XMPP server.

At the time of writing this article (January 2024), Ejabberd supports XMPP, MQTT, and SIP Service. Easy to say it almost offers everything to build the next WhatsApp just by using the community open-source version.

Ejabberd is designed with following design thinking :
- To be easy to integrate into existing application backend architectures
- With scalability and high-availability in mind
- To be as stateless as possible
- To allow customers to keep control of the data

The Ejabberd server is written in Erlang. It can handle millions of simultaneous connections. This is the old document but can give you a good idea of its scalability. Benchmarking document link here.

XMPP Federation Network
HTTP is a C2S (Client to Server) model, whereas XMPP has support for an S2S (Server to Server) model as well. Hence, users don’t need to be connected to the same server for communication. Servers can communicate between themselves, and messages can be delivered to users connected to other servers.

XMPP stack for quick prototyping
Server: Ejabberd
Client:
— Android: Smack
— iOS: XMPPFramework
— Web: Strophe
— Backend Integration: REST API, ejabberdctl CLI tool

XMPP Specifications
The best resource to learn in-depth is by going through the XEPs (XMPP Extension Protocols). They are published here in great depth.

What’s next?
The idea of writing this article is to give you an introduction about how we can build a highly scalable instant messaging, gaming, or such applications.
I may write the next article, which will include in-depth technical details about building applications from scratch using the Ejabberd server.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
If you want hands-on, then I have a working demo project, codebase setup with client and server in my private Github repository. It can be run locally using Docker. Please reach out to me in my email (narendra.kumar.soni28@gmail.com), or connect to me in my linkedIn.

References:

https://xmpp.org/
http://highscalability.com/blog/2014/2/26/the-whatsapp-architecture-facebook-bought-for-19-billion.html
https://www.ejabberd.im/index.html
https://prosody.im/
https://vimeo.com/44312354
https://www.igniterealtime.org/projects/openfire/
https://www.riotgames.com/en
https://about.meta.com/
http://highscalability.com/blog/2014/10/13/how-league-of-legends-scaled-chat-to-70-million-players-it-t.html
https://docs.ejabberd.im/admin/guide/mqtt/
https://community.element14.com/learn/learning-center/the-tech-connection/w/documents/4149/the-xmpp-protocol-benefits-architecture-and-applications

--

--

Narendra Soni

I am generalist and problem solver. My expertise lies in taking product from 0 to 1. I work hard to improve my knowledge. My DMs are open for collaboration.