RabbitMq for building mobile chats & messengers. Sharing experience
Tags: rabbitmq, appdev, programming
Published: 2017-12-06 13:58:38

---
layout: post
title: "RabbitMq for building mobile chats & messengers. Sharing experience"
published: true
tags: rabbitmq, appdev, techtalks, programming
cover_image: "https://thepracticaldev.s3.amazonaws.com/i/z3e91q0196zi1pjpfmho.jpg"
---

By now, mobile messenger development has gained significant popularity. Today, there are millions of mobile chats and messengers, with the most famous Viber, WhatsApp, or Telegram, each having its pros and cons.

To be competitive, such mobile apps have to be able to support heavy loads and be easily scalable up to millions (sometimes billions) of users, which creates certain challenges for developers. That’s why we recommend integrating ready and reliable messaging platforms in these solutions. 

Such integration will enable to transfer the solution to scalability problem to the messaging platform side and allow to pay more attention to new functionality development.

One of the platforms that is a good fit for messenger development is  </span><a href="https://www.rabbitmq.com/"><span style="font-weight: 400;">RabbitMQ, an open source multi-protocol messaging broker,</span></a><span style="font-weight: 400;"> which supports various messaging protocols like AMQP, MQTT, STOMP, etc.


RabbitMQ has a number of </span><a href="https://github.com/rabbitmq/rabbitmq-dotnet-client"><span style="font-weight: 400;">client</span></a><span style="font-weight: 400;"> libraries allowing it to be integrated with almost any client app platform.
 

<b>RabbitMQ has the following components:</b>


- producer – client which creates a message;

- consumer – client which receives a message;

- queue – unlimited queue that stores messages;

- exchange – component allowing routing messages to sent to it to different queues.
 

Generally, the interaction of the components within RabbitMq is the following: producer sends a message to exchange, then exchange receives the message and routes it to queues subscribed to it. 

Depending on the type of exchange messages can be filtered based on a correspondence of keys with which the queue is connected to the exchange and the keys in the message or routed to all interested queues, after which the consumer receives messages from the queue he/she is subscribed to.

 
<b>Types of exchange:</b>


- fanout – routes messages sent to it to all connected queues

- direct – routes messages sent to it to connected queues according to filtration settings using routeld (can be one word)

- topic – routes messages sent to it to connected queues according to filtration settings with using routeld consisting of a few words which allow achieving more flexible filtering.
 

<b>In general, the system includes the following components:</b>

- RabbitMQ;
- Backend application;
- Client applications: Android, iOS.


For the architecture to function one needs already set up exchanges and queues created at the first start of the backend application.
 

<b>The main exchanges and queues:</b>


<i>“conversation.outgoing”</i> is an exchange, type fanout, which in our case is required for receiving incoming messages from clients and routing them to the queues.

<i>“conversation.incoming”</i> is an exchange, type topic, for sending already processed messages to exchange of certain users. In our case topic type enables to send only those messages to users that relate to those dialogues they participate in.

<i>“chat-application-messages”</i> is a queue for processing incoming messages by backend application.


After initialization all used exchanges and queues we connect exchange “conversation.outgoing” with queue “chat-application-messages” and create backend message processor from queue “chat-application-messages”.

Integration of all parts of the system starts with user registration functionality.

When a user registers on the server using one of the clients, a backend application makes an exchange of a fanout type in RabbitMQ with unique generated name and returns this exchange name to the client application in which the user registered.

From then all authorized clients will also receive the name of this already existing exchange.

After receiving the exchange name of this user, the clients create a temporary queue in RabbitMQ, which exists only during the connection of the client with RabbitMQ message broker, and connect it with this exchange.

Creating one unique exchange for each user allows receiving messages from all clients (iOS, Android) at the same time.

After turning off the Internet on the client or in any other case when connection with RabbitMQ message broker is lost, a temporary queue will be automatically deleted on the server by RabbitMQ, thus preventing redundant messaging and optimizing our delivery. With a new connection, a new queue will be created.

After registration, the user is ready for both sending and receiving messages.

Sending messages takes place within dialogues. The system must enable to create conversations with both one and several contacts.

When creating a conversation, exchange of each user links with an exchange of the backend application “conversation.incoming”, using conversation id as routeId.


Check out more about <b></span><a href="https://smartym.pro/blog/using-rabbitmq-message-broker-for-messenger-development-some-clues-for-developers/"><span style="font-weight: 400;">using RabbitMq for creating mobile applications.</span></a><span style="font-weight: 400;"></b>

Also, get insight into </span><a href="https://www.rabbitmq.com/getstarted.html"><span style="font-weight: 400;">RabbitMq tutorial and installation guide.</span></a><span style="font-weight: 400;">


<b>A bit about user data security and privacy in mobile chat apps</b>


Concerning the development of mobile chat apps, there come security issues regarding user data privacy and communication security. 

At the moment end-to-end encryption (E2EE) is the most reliable way to protect user data and remove the possibility of hackers, governments, and security service to view any data, including chat messages.

Most popular mobile messengers have already take care of providing security. For intance, </span><a href="https://techcrunch.com/2016/04/05/whatsapp-completes-end-to-end-encryption-rollout/">WhatsApp integrated E2EE encryption in 2016 thus getting a competitive advantage</span></a><span style="font-weight: 400;">. 

Find out </span><a href="https://smartym.pro/blog/mobile-app-security-using-end2end-encryption-for-protecting-mobile-messengers/"><span style="font-weight: 400;">how to use end2end encryption for protecting mobile apps and ensuring data privacy.</span></a><span style="font-weight: 400;">