mobile app dev

Mobile messaging app development: Developing architecture for a chat application

 

Boom in mobile messaging apps

 
With the emergence and quick distribution of mobile communications, we have seen a boom in mobile messaging apps. Some like WhatsApp, Viber, SnapChat, and Telegram gained huge popularity and profits. Just imagine, $ 19 billion, the cost Facebook paid to acquire WhatsApp in 2014!

Surely, the achievement of such success is a complicated task, especially when it comes to high competition, trillions of existing products, and thousands of already implemented cool ideas.

Nevertheless, the number of mobile messaging apps is constantly increasing, each for its specific needs and goals: dating platforms, emoji chats, apps for searching people for hanging out, apps for internal business communication, and much more.

By the way, have you known that intelligence and police services are capturing criminals with the help of such chat apps like WhatsApp?

Representing a today’s everyday necessity, messengers can find a plenty of use cases. In this post, we’ll speak about their development, particularly, app architecture and best developer tools. And who knows, maybe it will help you build a cool product with millions of users?)

 

Messaging app development challenges

 
A mobile messaging app is about users and user communications. To achieve success (and at least not to fail), it has to be scalable up to millions of users and support heavy loads. The best option to solve these challenges is to integrate a messaging platform. This way enables to focus on the implementation of other functionality and make the product reliable and scalable.

Also, a modern world requires messages to be instant and secure. All communications should be protected (safe communication channels) and possibilities for collecting user information should be removed. Find out how to ensure mobile messaging app security by adding end-to-end encryption.

 

RabbitMQ in mobile messaging app development

 
RabbitMQ suits well for mobile chat app development. By integrating of this messaging platform into the architecture software engineers significantly simplify their work as well as get more chances to build a reliable and scalable solution.

In Smartym, we appreciate RabbitMQ for asynchronous messaging it provides as well as for other great useful features. The tool supports various messaging protocols (AMQP, MQTT, STOMP), allows message queuing and flexible routing, offers multiple client libraries, and ensures a reliable message delivery.

What’s important, RabbitMQ is officially supported on different operating systems and several languages. A mobile messaging app developed with RabbitMQ has the following architecture:

 
rabbitmq-architecture

 

RabbitMQ chat app development – Technical part

 
RabbitMQ tool consists of:

  1. producer – client that creates a message
  2. consumer – receives a message
  3. queue – stores messages
  4. exchange – enables to route messages and send them to queues

 
The system functions in the following way:

  1. producer creates a message and sends it to an exchange
  2. exchange receives a message and routes it to queues subscribed to it
  3. consumer receives messages from those queues he/she is subscribed to

 
One should note that messages are filtered and routed depending on the type of exchange. Let’s see how it works.
 

Types of exchange:

  1. fanout – routes messages to all connected queues;
  2. direct – routes messages to connected queues in correspondence with defined filtration settings and using routeld (that consists of one word);
  3. topic – routes messages to connected queues in correspondence with defined filtration settings and using routeld (consisting of several words which makes filtering more flexible).

 
The main queues and exchanges needed for the successful functioning of mobile messaging app architecture:

  1. “conversation.outgoing” is an exchange referred to a fanout type. “Conversation.outgoing” is generally used for receiving incoming messages from clients and routing them to connected queues;
  2. “conversation.incoming” is an exchange referred to type topic and serving for delivering processed messages to connected exchanges;
  3. “chat-application-messages” is a queue serving for processing incoming messages by a backend application.

 
The integration of all system parts begins with user registration on the server using a required client (iOS, Android). Once a registration process is completed, the user can send and receive messages.

Then, when registering, a backend application creates an exchange (type fanout) in RabbitMQ with a generated name. All authorized clients get this unique name, build a queue (existing only within the connection between client and RabbitMQ), and link it to the exchange.

One should note that the creation of a unique exchange for each particular user enables to simultaneously receive messages from all the clients.

In case of losing the connection with RabbitMQ (for example, when the Internet turns off on the client), the queue is automatically removed on the server, which allows preventing redundant messaging and ensures a reliable message delivery.

Conversations (sending and receiving of messages) occur within dialogues. When creating a new conversation, the connection between an exchange of each user and an exchange of the backend application “conversation.incoming” is also created. The message delivery, in this case, looks like it’s shown in the scheme.

 
scheme
 

Now let’s get a deeper insight into the entire functioning. First, incoming messages move to a “conversation.outgoing” exchange for further sending to a “chat-application-messages” queue. Then, all messages in this queue are checked by the backend application.

Dustin the process of message processing, the system checks such things as user token validity, conversation id, a user capability to send messages to the required conversation, attachments included in the message, and so on. Messages are stored in the backend application database and sent to a “conversation.incoming” exchange with conversation id as routeId.

Here the filtration of all sent messages enables to easily scale the number of user contacts that participate in dialogues with no need to increase the time required for message delivery. Thus, RabbitMQ helps provide instant messaging during chat app development.

 
The backend application (Android)
 
Today, mobile messaging apps are first of all about instant messaging. What’s more, the application must provide reliable message delivery in both online and offline modes.

Concerning these challenges on our practical experience, we built an Android chat application, in which we implemented a background system service, that constantly checks the queue created for new messages.

To develop this functionality, we applied a class using an Android IntentService standard. It builds a connection with RabbitMQ server and a temporary queue. Take a look:

 
connection = ChatApplication.getApp().getRabbitMQConnectionFactory().newConnection();

channel = connection.createChannel();

queueName = channel.queueDeclare().getQueue();

channel.queueBind(queueName, ChatApp.getApp().getAppConfig().getExchangeName(), “#”);

QueueingConsumer consumer = new QueueingConsumer(channel);

channel.basicConsume(queueName, true, consumer);

while (true) {

QueueingConsumer.Delivery delivery = consumer.nextDelivery();

handleMessage(delivery);

}

 
A handlemessage method includes a code for processing messages and then adding them to the local client base and displaying messages on the UI. Then, to ensure reliable message delivery, the application makes a new connection with RabbitMQ and sends it to “conversation.outgoing” exchange in JSON format.
 
Connection connection = СhatApp.getApp().getRabbitMQConnectionFactory().newConnection();

Channel channel = connection.createChannel();

try {

Map<String, Object> headers = new HashMap<>();

headers.put(ACCESS_TOKEN_HEADER, getPreparedUserToken());

AMQP.BasicProperties properties = (new AMQP.BasicProperties()).builder().replyTo(MessageService.getTempQueueName()).headers(headers).build();

MessageBean message = new MessageBean();

message.setTemporaryId(mTemporaryId);

message.setAttachments(attachments);

message.setType (type.getType());

Gson gson = new Gson();

String messageJson = gson.toJson(message);

channel.basicPublish(OUTGOING_EXCHANGE, routeId, properties, messageJson.getBytes());

 
Thus, the integration of RabbitMQ into the architecture enables to build scalable mobile messaging apps as well as allows engineers to focus on the adding business logic, simplifying the entire development process.
 
Having a lot of mobile chat apps successfully built and delivered, we are ready to help you with your project. If you have some questions or a mobile app idea, apply to us and get a free consultation!