We have a python backend that is querying an API (text/event-stream) and returning that same type so the response is streamed as it is received.
Our frontend is React, and when it queries the python backend the answer is streamed successfully through.
We also have a .NET backend, that handles more business logic, and that call now needs to be made through that.
So it’s basically: React -> .NET (API) -> Python (API)
Here is the code snippet that makes the call from the .NET API to the Python API – it works, and streams it to the frontend, but it has a few second delay, where it had close to no delay when it was going from React -> Python.
// using HttpClient for web requests
var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
var streamResponse = await response2.Content.ReadAsStreamAsync();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = await streamResponse.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
string data = System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead);
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
await Response.Body.WriteAsync(dataBytes, 0, dataBytes.Length);
await Response.Body.FlushAsync();
responseFromQuestion += data;
}
await Response.CompleteAsync();
Is the problem this line where it awaits perhaps?
var streamResponse = await response2.Content.ReadAsStreamAsync();
Please help – thank you!!
As stated above, the code works, but there is a noticeable delay (4-6 seconds), where before introducing the .NET had almost a 0 delay.