RxJava2 Demo 1- Facebook Live Video Emoticons Streams.

Anshul Jain
MindOrks
Published in
3 min readFeb 3, 2017

RxJava is the buzzword now a days. A lot of developers are trying their hands on RxJava. My first tryst with RxJava started about a month ago, when one of my colleagues forwarded a book to me. I started reading the book, but found the concept of RxJava really hard to grasp initially. So I decided to use RxJava in a project. Since RxJava is all about streams, I decided to use RxJava to make a project which is similar to the flow of emoticons in Facebook Live Video.

About the Project:

The project is built for emulating the Facebook Live Video feature where the emoticons flow on the screen. There is however, one condition that I have kept- every emoticon should flow at least 300 ms after the previous emoticon. I am using RxJava2 in the code. Similar to Facebook, I have used 6 emoticons in the project, but for the purpose of explaining, I will show how the like and love button works and generates a stream of events.
Lets deep dive straight into the code!!

Creating a Stream from click events:

In RxJava, everything can be converted into stream. Therefore the clicks on various emoticons can be considered as one single stream which will emit different types of items at different intervals.

Creating a Flowable:

Since we need to check the time difference between two stream items, we need to get the timestamp information from every item.

First, we create a Flowable from FlowableOnSubscribe. I am using Flowable in place of an Observable because I need to have a back pressure strategy which is used when the subscriber is not able to receive the events as fast as the source is emitting.

In the above code, I am keeping the back pressure strategy as BUFFER. So when the source is producing events faster than the subscriber can consume, the stream items will be buffered. Next we convert this Flowable to Flowable<Timed>. Thus every item will have timestamp information along with it.

Creating a Subscriber:

Now, since we have a Flowable, we need a subscriber to subscribe to the events generated by the Flowable.

A subscriber has 4 methods namely onSubscribe(), onNext(), onError() and onCompleted().

In the onNext() method, we need to get the timestamp of the last emitted item and if the difference between the current timestamp and last emitted timestamp is greater than 300 ms, then only we request one more item from the stream. Else, we asynchronously wait for 300 seconds to complete before requesting for the next item.

Unsubscribing from the Flowable:

It is very important to cancel the subscription, once the subscriber is no longer interested in the stream.

Since this is my first project on RxJava, there might be a few things which I could have done more efficiently. Please feel free to drop comments where ever you feel the code can be improved.

The project is hosted on Github. Please star the project if you find it useful. This will keep me motivated for working on more projects. And thanks for reading the article. And let’s connect on LinkedIn.

For more about programming, follow Mindorks , so you’ll get notified when we write new posts.

Check out all the Mindorks best articles here.

--

--

Anshul Jain
MindOrks

ex Android Developer | Machine Learning Developer @ Dailyhunt