A Journey to Implement WebSocket in Your Android App Using Scarlet

Anang Kurniawan
Staffinc Tech
Published in
3 min readJan 5, 2022

WebSocket is one of the options for implementing the data layer in Android applications, especially if the application requires data updates in real-time. This article will discuss how to implement web socket into Android applications quickly.

In my journey, I had built many apps with different use cases. But none of them require real-time data changes. That makes me never touch any libraries that are suitable for real-time data changes.

Last week I just got an offer about a project from my colleague that needs an implementation of real-time data changes, and he asked me if I could do that or not. Feeling challenged, I accepted that offer. And now, I want to share my experiences in implementing Web Socket on my android app.

After spending some time exploring how to implement a web socket in the android app, I found a helpful library that works great in my case. It is Scarlet, A Retrofit-inspired WebSocket client for Kotlin, Java, and Android by Tinder.

Because Scarlet is very similar to Retrofit, which I have often used, the setup process is straightforward. Let’s see how to set up Scarlet in our android project.

Setup!

To implement Scarlet, first add it to your app/build.gradle.

In the example provided by Scarlet, RxJava is used to help manage the stream of data sent by the web socket. Implement it in app/build.gradle.

If you curious about Coroutine and Kotlin Flow support, see here: https://github.com/Tinder/Scarlet/issues/114
I haven’t tried it, but best of luck!

Now the essential part of our setup is the service. We can test our WebSocket implementation using websocket-echo (wss://websocket-echo.glitch.me)

Next, create an interface to define a function to communicate with the WebSocket.

As seen above, the service interface looks very similar to retrofit. You can use @Receive annotation to observe data stream from WebSocket, and use @Send to send or subscribe data to WebSocket.

After setting up our service, we can continue to implement our service in our activity; in this case, I am not using any architecture, so we just implemented the service in the activity class.

all configuration used in the above snippets are just basic configurations. You can find the more complex configurations in this article

or just see the sample provided officially by Scarlet in this report

Testing!

Now we can test our code by sending something to the WebSocket. The WebSocket will send the same message as they retrieve.

You can see the result in the video below:

That’s all we can discuss in this article. Scarlet is a great library that gives you access to implement WebSocket in your Android app. Setting up Scarlet is easy, especially if you are already familiar with tools like Retrofit.

If you have any questions or anything to discuss, please kindly post it in the comment section, and for the more detailed code as mentioned above, you can see it in this report:

About The Author

I am an Android Developer who loves the world of arts. I work as an Android Developer, but sometimes I do a design challenge with my friends to fill my spare time.

LinkedIn Medium Dribbble Twitter Instagram

--

--