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

Mayur Rokade
5 min readJul 10, 2018

--

In this article we will dive into code and learn how to build a realtime app using Android MVP. As we progress, we will learn how to implement an app using Android MVP Realtime architecture.

This is part two of Android MVP Realtime Architecture series and

1. MVP vs MVP Realtime

Before we go any deeper into the code jungle, it would be wise to take look at the architecture from a bird’s eye view.

Android MVP using Retrofit. Image credits — Mayur Rokade

From the above image, we see that MVP uses webservice(Retrofit internally)to make REST API calls. Only the client can initiate a conversation with the server. Whereas the server cannot initiate a conversation with the client. In short, the conversation is one-sided and not bi-directional.

Android MVP Realtime using Socket.IO. Image credits — Mayur Rokade

MVP Realtime uses eventservice(Socket.IO internally) to send and receive events. In this case, the client establishes a persistent connection with the server. This allows the server to initiate a conversation and send events to the client. In other words, the conversation between the client and server is now bi-directional.

2. Dude! Show us the code!

Well, this is going to be quite a lengthy article. If you dying to see some code, you can directly head over to Github for the source code. The link is in the card below. For the rest, keep reading for the adventure that lies ahead.

We will work our way backwards from EventService -> RemoteDataSource -> Repository -> Presenter -> View. This will make sense as we progress.

Fair Warning! I will be covering only the key elements that make the app realtime. So please don’t copy paste the code and expect it to work.

3. EventService

Quick look at the below project structure image tells us that, the eventservice package contains three files:

  • EventService
  • EventServiceImpl
  • EventListener
Project structure

EventService is the service layer that connects/disconnects to the server and sends and receives events too.

EventServiceImpl is the implementation of the EventService interface. This class using Socket.IO to establish a persistent connection with the server.

EventListener is the interface to handle incoming events. This interface will be implemented by DataSource, Repository, Presenter and View. As we will see, that this interface plays a very important role in making the app realtime.

4. DataSource

Datasource, as the name suggests, is the interface to access data. In this architecture, DataSource extends EventListener so that it can also listen to incoming events.

5. RemoteDataSource

RemoteDataSource talks directly to EventService. For eg. to send a message, the RemoteDataSource can call mEventService.sendMessage(). It can also listen to incoming events and pass the data back to the Repository using mRepoEventListener.

6. Repository

In this case, repository also does something similar to RemoteDataSource. It talks directly to RemoteDataSource. For eg. to send a message, the Repository can call mRemoteDataSource.sendMessage(). It can also listen to incoming events and pass the data back to the Presenter using mPresenterEventListener.

7. View and Presenter

The View implements the EventListener interface because that is where the server sent events land up as UI updates. Finally!

Presenter implements the EventListener interface listen to incoming events from Repository. These events are passed back to View via mViewEventListener.

8. The Role of EventListener

As you can see from the above examples, its the EventListener that plays a very important role in passing the data from EventService to View. The EventService doesn’t need to know about M or V or P or Repository. It only needs access to EventListener to pass back the data.

9. What about Persistent Connection?

For an app to be realtime, it needs to have a persistent connection with the server. But an Android app has activities, that are started and destroyed. So, how do we make sure that, our connection is not interrupted even if we switch between activities. Also, we want the connection to be active only when the app is in foreground. When the app goes into background, the connection should be terminated.

Based on above constraints, the code to create or terminate a persistent connection can’t be part of an Activity or a Fragment. However, it can be a part of the Application class.

Now, if we choose to put out persistent connection code in the Application class, how does it detect if the app is in foreground or background?

The solution to detecting if the app is in background or foreground can be found in Android Architecture Components. More specifically, LifecycleObserver.

And, this is how LifecycleObserver can be used in your Application class.

This is it! This is where the code part ends. Like I said this, this is quite a lengthy article. I have created an app to showcase how this architecture can be used to build realtime apps. It would have been pointless to talk about it without a working example. So here it is!

10. Socket Chat — Built using MVP Realtime

Socket Chat is a demo app built using Android MVP Realtime architecture. It connects to Socket.IO chat room at https://socket-io-chat.now.sh. If you want to give it a try, just clone the repo and build the app using Android Studio.

Checkout the source code at Github

11. Socket Chat — Quick demo

If you want to see the app in action right away, click in the below YouTube video. In web, I have opened the Socket.IO chat room https://socket-io-chat.now.sh. The app connects to the chat room and can seamlessly exchange messages.

12. What’s next

Unit tests! I am still working out on how to mock responses for a Socket.IO server. Another thing to I would explore is RxJava backpressure. Consider a scenario where there are too many incoming messages which may cause too many UI updates.

Disclaimer: This is just a proposed architecture and is not to be taken as a standard solution. There are many improvements possible. So suggestions for further improvements are most welcome.

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

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