Saying goodbye to my daddies of American literature

You are dead and perhaps reincarnated somewhere living your soul’s purpose one way or another, perhaps even reading your own work from a previous lifetime and feeling something or nothing about it…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




WebSocket on Adroid with OkHttp and RxJava

Just to explain it with an example, imagine that you want to write a client chat app and in the simplest implementation ever, every time your users send a message, the message is stored in a database server side and your app needs to read it from the server.

Imagine that the first user sends a message, this message is stored, now your client app needs to ask the server to give the message back and show it in a chat interface; but you don’t know when this information will be available on the server (so when the other user will answer) and the only way your client app has to know whether there are messages is it to constantly and periodically interrogate the server with “do you have messages I need to show?”, “do you have messages I need to show?”. This is called polling and it is very expensive. With WebSockets the server is able to send message to the client without being first requested from the client and allowing messages to be passed back and forth while keeping the connection open.

Now that we know how a WebSocket works, in this article I will explain how to write a WebSocket for an Android App using OkHttp, RxJava in Kotlin.

In Android we can use a library called OkHttp, which among other things can help create a WebSocket connection. So the first step is to add the library in the gradle build file and add internet permission in the Android Manifest.(When I first started the implementation I used version 3 but as far as I know there is also a version 4 of OkHttp)

When creating the connection to the WebSocket we are required by the library to write a listener that will take care of opening/closing the web socket connection alongside managing the messages received. The main part of the implementation would look like that:

When we open the connection we are then required to pass an OkHttp Request and the listener created.

And that’s all we need to do to manage the WebSocket connection as the library will take care of most of the communication.

The Android App I wrote follows an MVP architecture pattern and makes heavy use of RxJava so this means that the whole logic will be in the Presenter Layer and ideally every time I open/close a WebSocket connection or I receive message from the sever on the WebSocket my Presenter receives an event which will be then handled. The App will be super simple, I want to track when I receive a Welcome Message, which will be sent after the handshaking with the server, a Ping Message, which I will use to just check that I still have an active WebSocket connection and an Update Message which will be the sign for my Presenter to go and fetch new data (ideally my server will send an Update Message every time the client is required to update itself.)

In order to do that I created two sealed class. The first one will map the kind of message that I receive from the WebSocket into something that my App can understand and the second one is the list of all possible events that my WebSocket handler class will eventually send to the Presenter.

The WebSocket handler class will look like the following:

The WebSocket handler class is a Singleton and as you can see every time the onOpen method is called I check whether there is already a WebSocket connection and eventually I will create a new ObservableEmitter for every event the Presenters are required to subscribe to (WebSocket open/close, ping or update). This emitter is then added to a list of ObservableEmitters, called subscribe. Every time an emitter is disposed a Runnable is run which will check and remove from the list all the disposed emitters and in case subscribe is empty it will close the WebSocket connection.

Once the WebSocket handler class has been defined the only thing remained is to describe how the presenter will react to WebSocket messages. The main part of Presenter that will handle the WebSocket will look something like:

In the example above OnNetworkConnectioChange will return an Observable<Boolean> to indicate whether there is or not a valid Internet connection and in case there is a WebSocket connection will be created. All the messages arrived on this connection will be handled in the subscribe section.

Add a comment

Related posts:

Government Services Drive Digital Identity Growth

Digital identity is a concept that is constantly evolving and becoming increasingly aligned with allied areas, such as security, privacy, and management of identity-related data. Within the…

Thorough Thursday

Every time my internet goes out I remember I don’t have a backup plan, and I panic. Don’t I know I’m supposed to have a backup plan. Must set up hotspot. And that reminds me of the hundred other…

Power And Personality

The personality characteristics of a group’s membership or the overall general personality orientations of the members of a group heavily influence that group’s ability to solve problems. To a…