java - 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:
- 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.
- 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:
- 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.
- 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.
- 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.
解决方案
差分法实现:
- 在 Node 端,使用
fs.createReadStream
函数进行流式传输。 - 在 Android 端,使用
URLConnection
,BufferedReader
,InputStreamReader
来消费流。
PS - 没有任何办法通过改造来做到这一点。
推荐阅读
- bash - 获取使用 $() 执行的 bash 命令的退出代码
- python - 将 pandas df 列拆分为多列
- css - div不使用高度自动扩展高度
- xamarin - 在 Azure devops microsoft 托管构建服务器上安装新版本的依赖项
- prolog - Prolog:使用可选事实实施规则
- go - 如何在golang中围绕我的数据绘制一个框并打印它?
- html - 在谷歌日历(python)上找不到 SVG 元素
- javascript - Bootstrap 5 工具提示、弹出框和 toast 在 Ruby on Rails 6 中不起作用
- php - 在 Linux 服务器上从 PHP 调用 R 脚本
- php - 如何在 XML 子标签中插入数据