airbnb / hypernova

A service for server-side rendering your JavaScript views
MIT License
5.82k stars 208 forks source link

Response streaming #89

Open Inkdpixels opened 6 years ago

Inkdpixels commented 6 years ago

As of React 16 a new method renderToStream() method was introduced. I first though of creating a feature request issue at airbnb/hypernova-react but after taking a look at the code it seems that more ground-work needs to be done at the server itself to support streams in the first place.

@goatslacker mentioned in #22 that there are plans to support streams and even async rendering, it would be interesting if this was already started by anyone or if we could support you guys on this feature.

In the latter case it would be nice if we could collect all ideas/hints here on how to implement this feature correctly.

ljharb commented 6 years ago

* will be introduced - React 16 isn't out yet.

Definitely we'd like hypernova to support response streaming, and when available, to take advantage of renderToStream!

goatslacker commented 6 years ago

React 16 is out in beta right now I believe and the stream/async rendering has already landed so feel free to take it for a spin and report back what works/doesn't work/what you'd like to work/how @Inkdpixels

patrick-mcdougle commented 6 years ago

What's the benefit of rendering to stream when all the markup has to be buffered to send the HTTP response to the hypernova client? Am I missing something here?

ljharb commented 6 years ago

@patrick-mcdougle it doesn't all have to be buffered; by simply beginning to buffer it, chunks can be immediately sent down to the client, before the rest is ready.

patrick-mcdougle commented 6 years ago

@ljharb But the client is almost always some legacy application, which needs the JS view rendered. The hypernova client isn't ever a browser, is it?!

ljharb commented 6 years ago

There’s sort of a client (browser), a combo client/server (whatever talks to hypernova and the web), and a server (hypernova). The webserver absolutely might support streaming - rails 4+, for example - and the browser always does - so hypernova would have to stream also to be able to support streaming through the entire process.

patrick-mcdougle commented 6 years ago

I guess, but the whole response from hypernova will have to be re-tooled, since the response is JSON. You can't json_decode a partial JSON object...I guess I'm not saying it's impossible, but is it worth the effort?

ljharb commented 6 years ago

indeed, we'd have to use json streaming, or something similar.

I'm not sure if it's worth the effort yet - but it seems likely.

MichalCz commented 6 years ago

Well - looking from outside - you can indeed decode a JSON Object using JSONStream - I made quite a lot implementations using that in reading - in writing my Scramjet..toJSONArray will easily deliver a JSON array (or object via another method) items in realtime while still provinding a proper JSON output for legacy and API use.

Can you elaborate a little on the code - maybe find a nice place to make a proof of concept? I could try and help.