Unable to Consume Webflux Streaming Response in React-Native client

你说的曾经没有我的故事 提交于 2019-12-11 11:52:21

问题


I've a reactive streaming api built on spring 5 webflux running on netty server with the following syntax -

@RequestMapping(path = "/customer/messages/stream", method = RequestMethod.POST, produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Flux<Messages> fetchAllMessagesForCustomer(@RequestBody CustomerRequest)

Now to consume this from my react-native app, I tried using fetch Api but it didn't work because of react-native issue. So I tried with good old xhr

xhr.onreadystatechange = function() {
  console.log("state change.. state: "+ xhr.readyState);

  if(xhr.readyState == 3) {
    var newData = xhr.response.substr(xhr.seenBytes);
    console.log("newData", newData);
    xhr.seenBytes = xhr.responseText.length;
  }
};

This works to some extent but with the following issues

  1. Response consumption is not sync with publishing streams and most of the times multiple streams get combined and thrown to the client.
  2. xhr.responseText essentially appends all the streams and keeps growing bigger which seems like an anti-pattern as the whole idea was to break down huge responses into smaller chunks and consume them individually.

Is there any good alternative to handle the streaming response in react-native ? Suggestions are welcome.

来源:https://stackoverflow.com/questions/51112158/unable-to-consume-webflux-streaming-response-in-react-native-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!