Reputation: 12112
In my express app, I want to make multiple calls to an API and stream each response back to the client as and when I receive it, instead of waiting for all of them.
For example, if I make requests to yelp for restaurants in San Francisco, Berkeley and Palo Alto in parallel, I shouldn't have to wait for all the responses to come back and be able stream them as they're available. How would I do this?
Upvotes: 0
Views: 956
Reputation: 20387
Since browsers wait till the entire response is received before passing the result to javascript, this isn't directly possible. You could, on the other hand, do it with websockets.
Possible architecture:
{api: "yelp", data: ... }
{ api: "yepl", done: true }
I highly doubt this is a good idea. It's far more complex and you'd be better off using 4 parallel requests from the client, or if possible, querying the apis directly from the browser.
Upvotes: 1