ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Member-only story

Android: WebSockets made easy

--

Found on howtogeek.com

Handling REST API calls is a common thing in Android apps nowadays. Most developers will be familiar with libraries such as Retrofit, making our lives easier when it comes to consuming these APIs. Websockets on the other hand can be quite a hassle, but what if there’s a Retrofit equivalent for just that?

Consuming APIs, we’ve all been there. We do a request and receive a response, pretty straightforward right? With WebSockets however, we have an open connection to the server. Data can be passed around at any given moment, which can be useful for things like chat or real-time trading, but this does leave more room for error. What do we do when the app goes to the background? What about when the WebSocket connection fails or drops? This is where the Scarlet library comes in.

Enter Scarlet

Scarlet, a Retrofit inspired WebSocket client, manages the client-server connection for you. It makes use of a StateMachine to handle our WebSocket connection correctly.

Found on https://github.com/Tinder/Scarlet

By using Scarlet, we don’t have to worry about reconnecting at all. When initializing our WebSocket we tie it to our lifecycle and are even able to define a backoff strategy, making Scarlet…

--

--

Responses (8)

Write a response