4

I am going to create a stock market android app. Stock quotes change every few seconds, so to keep the app updated I decided to implement a reactive functional design.

My IDEAL design would be:

  • Everytime that the server has a new value for a stock, the app is passed the new data. Finally, the data is displayed to the user.

However, my problem is that my source for stock data is a REST api (yahoo's, for example). With a REST api, I think that the "notify the app" part of my design won't be easily implemented.

I thought of doing:

  • Schedule an Api call every X seconds using Retrofit + RxJava. While this seems to be the simplest way, there are 2 things that I don't like about it:

1) I'll be downloading much more data than is necessary, since I don't know what stocks have changed their value, I will have to fetch all of them.

2) This is not really "reactive programming". I somehow feel that the server should "push" the data to the client and not the other way around.

Can you give me some pointers as to how would I implement the "Ideal" design that I described above?

2
  • 1
    Have you looked at ETag and If-None-Match? I would create an Observable that uses a thread to poll the address. If you get a 304 Not Changed, then you don't call OnNext on your Observable. For this to work, you'd need to be able to query each stock one by one...
    – ArTs
    Commented Jul 18, 2016 at 3:33
  • 1
    Don't put your Google Search in the title of your post. Instead, summarize the question you are asking in a dozen words or less. Commented Jul 19, 2016 at 14:18

2 Answers 2

2

Everytime that the server has a new value for a stock, the app is passed the new data. Finally, the data is displayed to the user.

This leaves a lot out of the story. This is more a use case than a design.

There are several ways to approach this problem. It's your fundamental limitations that really guide the design. I'll be making assumptions about what your limitations are.

Presumably you don't own the server that is the source of stock data. You simply have a rest API you can connect to. Likely you are limited in how much you can download and how often you can connect.

Schedule an Api call every X seconds

This is called polling. It has severe limitations over events but can work well if done well.

1) I'll be downloading much more data than is necessary, since I don't know what stocks have changed their value, I will have to fetch all of them.

That depends entirely on the limitations of the API. If the API really won't let you inquire about a particular stock but insists on sending you the whole ticker then yes this is true.

2) This is not really "reactive programming". I somehow feel that the server should "push" the data to the client and not the other way around.

Polling, done right, is actually what events really are. It would be an awful design if every x seconds every user hit the API and searched through the ticker list for the stock they cared about and checked if the price had moved. That's a lot of work and pointless moving of data.

If the API won't let you do anything more advanced then pull down the whole ticker when you call it I recommend setting up a server of your own. Your one server will hit the API of their server, get the whole ticker, do this every x seconds, but will be the only thing doing this.

Your server will allow users to register as observers of stocks and every time a stock changes your server will loop that stocks registered observers and push a notification out to them.

You'll need some reasonable timeouts and limitations on registering as an observer or you'll be the subject of denial of service attacks.

Done this way notifications can be limited both to stocks of interest, price movements of interest, and with timeouts, to observers still interested.

This would also allow you to push notifications out in any form you wish, UDP, TCP, SMS, email, phone calls, post cards.

5
  • Seems like a job for an RSS feed. Commented Jul 18, 2016 at 1:53
  • @RobertHarvey I gave up on RSS long ago. My current solution is changedetection.com Commented Jul 18, 2016 at 1:57
  • @CandiedOrange I like your solution very much. Can you point me to some resources on how to actually implement it? Keep in mind that I have never implemented a server of my own. Can this be done with node.js? Would it be easier of harder to do using node? Commented Jul 18, 2016 at 8:30
  • The single defining characteristic of server code is stability over time. You can't leak memory. You can't slowly gobble up resources expecting that a restart will clean things up. Server code has to clean up after itself. Here's an article about creating a server with node.js with a REST API you can define. Server side Javascript is relatively new compared to Java or Python. That's bound to be limiting but if that's how you prefer to work there is a movement of Javascript programmers going server side that you can follow. Commented Jul 18, 2016 at 12:35
  • Thank you for your advice, it is really helping me. One more question, if you don't mind. If I were to use Java for my server, could you point me to some guide that helps me to implement: 1) polling data from the api with my server. 2) Subscribing clients to my server. 3) Notifying the client when the server has new info. 4) Pass the data to the client using JSON. Sorry for my ignorance but, as I said, I have never programmed server side. Commented Jul 18, 2016 at 20:22
0

You can do the polling (maybe with some batching / caching for multiple clients) on the server side. To notify the mobile apps of changes, you can use Google Cloud Messaging and save the clients some bandwidth.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.