Android MVP — Realtime Architecture with RxJava and Socket.IO — Part 1

In this article we will discuss what realtime apps can do and how we can use MVP to build a realtime app.

Mayur Rokade
May 20, 2018 · 4 min read

This is part one of Android MVP Realtime Architecture series and

Overview

Most of us Android developers have created apps using the MVP architecture. A regular android app involves making HTTP api calls to the server, getting some data and rendering the view. Using these API calls, you can accomplish things like

  • Booking a cab
  • Ordering lunch
  • Transferring money etc.

As you can see, HTTP APIs are quite simple, scalable and you can get a lot of things done with them. Once you learn MVP with RxJava and Retrofit you are ready to take on the world with your amazing app. Yay!!

This feeling of awesomeness is quickly replaced by confusion the moment you try to build realtime apps like

  • Chat app
  • Multiplayer gaming app
  • Realtime stock price updates app etc.

1. What is a Realtime app?

Let’s say, two players are playing poker via an Android app. If player one makes a move, the other player is updated immediately. Similarly, in case of a chat, if one user sends a message, the other user gets the message immediately. And that is what a realtime app is.

2. Realtime flow of events

If we try to draw a simple architecture for realtime flow of events, it would look something like this.

Android users communicating in realtime.

The flow of events is bidirectional. The user can send events to the servers and at the same time server can send events to the user. So simply put, a realtime app would an app which is capable of sending and receiving events.

3. Hey! I use Retrofit and OkHttp. How do I make my app Realtime?

TL;DR. You can’t.

Now, Retrofit and OkHttp, both of these libraries use HTTP as the transport protocol. HTTP follows the request-response model. Here, the client sends a request to the server and server gives back a response.

The client can always reach the server. However, the server can’t reach the client. This is because, HTTP is not bidirectional. This very nature of HTTP prevents us from building a realtime app.

One can argue, that you can continuously poll the server. That way you can get faster updates. But polling is not very resource efficient. And when it comes an Android app, polling can drain battery, give a bad UX and might cause the user to uninstall the app.

Now, if the server wants to send events to client, it can happen via push notification. Push notification is not part of HTTP. To enable push notification, you need 3rd party libraries like Firebase or APNs. Without these libraries, using regular HTTP client-server setup, its just not happening.

4. Socket.IO to the rescue!

Socket.IO uses websockets and follows the pub/sub model to send and receive events. Pub/Sub means publisher/subscriber. Here, a publisher is an entity that generates an event and publishes it on a topic. A subscriber is an entity that listens to the topic and consumes the generated events.

Pub/Sub, unlike HTTP, allows the communication between two entities to be bidirectional.

So, technically speaking, any library that follows pub/sub can be used to make a realtime app. Some common libraries that help you with this,

  • Socket.IO
  • Pusher
  • Firebase Cloud Messaging

For the sake of this article and code examples I will stick with Socket.IO.

5. Realtime Android MVP Architecture with Socket.IO

In Android MVP, view is responsible for rendering the UI, presenter is responsible for presenting the data and repository is responsible for getting the data.

So for the app to be realtime app, it should be able to publish and subscribe to events. In simple terms, send and receive events. Based on what we know, if we try to draw an architecture, it would look some thing like this.

Android MVP Realtime Architecture

For the app to be realtime,

  • The Repository and the Server, should be able to send and receive events.
  • The Presenter and the Repository, should be able to send and receive events.
  • The View and the Presenter, should be able to send and receive events.

Whoa!

To Conclude

In this article we explored MVP Realtime architecture. In part 2 of this article, we will implement an android app that uses MVP, RxJava, Socket.IO and Realtime architecture.

Stay tuned for part 2 of Android MVP — Realtime Architecture with RxJava and Socket.IO. Let me know your thoughts and goodbye for now.

Happy coding!

Want to build a Mobile app? Reach out to me.

If you have an idea and want to convert it into Android app, do get in touch with me. Apart from Android development, I have helped people in identifying customers, figuring out business model and other startup related stuff.

You can reach out to me at:

Thank You!

Mayur Rokade

Written by

IIT Roorkee. Sr. Android Developer. 5+ apps on Play Store. ex-Directi, ex-LinkedIn.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade