首页 > 解决方案 > Stream binary data (created using FlatBuffers) from Node backend and receive in Android frontend

问题描述

Technology Used:

In the question below, "frontend refers to Android" and "backend to Node.js".

Constraints:

  1. We have rural users in developing market, so the internet may be slow and/or jittery/unstable. Due to jitter, we need a solution where we can use whatever (if not all) data is transmitted.
  2. We have quite large data (huge list of objects) which we cannot simple transmit through JSON (via plain REST APIs), as until the whole data is downloaded, we get nothing (because we are using Retrofit and its onResponse is not called).

Goal:

  1. To convert the list of objects (in backend) to binary data. So that when we receive data in the frontend, we are able to access serialized data without unpacking. Achieving it through FlatBuffers.
  2. To transmit this data through streaming when triggered from the frontend. I want to stream the data as I want to use (show in UI in realtime) whatever data (list of objects) user has received (Even if user gets disconnected during transmission). I am having issues here, as I am unable to achieve this through REST API - Retrofit combination. Need help here about what to use for trigger based streaming.
  3. To reconvert the list of objects in the frontend to Java objects and show in user's UI. I am using FlatBuffer here, as it is fast and able to use/serialize whatever objects are transmitted. No need for entire data transmission to complete.

I am able to successfully implement step 1 & 3 of the goal. But, I am not able to sort out step 2.

Please suggest what is a good and easy way to achieve this (stream binary data from backend to frontend). It would be better, if we can trigger and stream using Retrofit (if possible) in the frontend.

标签: javaandroidnode.jsreststream

解决方案


差分法实现:

  1. 在 Node 端,使用fs.createReadStream函数进行流式传输。
  2. 在 Android 端,使用URLConnection, BufferedReader,InputStreamReader来消费流。

PS - 没有任何办法通过改造来做到这一点。


推荐阅读