3 ms·
The whole idea of having the response be part of the input for the route seems very 2010s. Nearly everything else these days has a route that takes a request a
by nailer 2y ago
The whole idea of having the response be part of the input for the route seems very 2010s.
Nearly everything else these days has a route that takes a request and returns a response. This matches HTTP more closely.
- aphantastic 2y agoCuriously, the express model ends up being a more clean style when creating long lived writer streaming endpoints, which is becoming much more popular with LLM’s. All that was old will be new again.
- bhouston 2y agoAre they using SSE? Or web sockets?
- aphantastic 2y agoThe standard set by openai and shared by everyone else is to use SSE when “stream: true” is set. Personally I like to strip all the excess data as soon as possible and expose the raw response as a plain “transfer-encoding: chunked” stream. That way the client can decide if they want to accept each chunk as they come or simply await the whole response with limited code change. Either way, being able to easily pass the response writer around and set headers, write, close, as needed without being tied to a single function’s control flow is nice. It can be done in the more modern frameworks ofc, but ends up being a bit more machinery required at each endpoint.
- deleted 2y ago[deleted]
- nailer 2y agoA generator function that yields responses would fit that scenario perfectly.
- aphantastic 2y agoNot that perfectly. You still have the question of how to represent the one-time header data.
- nailer 2y agoI'm imagining: yield the headers (Content-Type: text/event-stream) the first time, yield the newline-formatted event streams on subsequent yields. I haven't done it though, I used to really like SSE (it's a little more REST-y) but the world seems to have decided on websockets.
- johnny22 2y agoI think that's how it works with nestjs's observer setup with rxjs. I just return the SSE stream and it works. I use it with fastify, not express.
- aphantastic 2y agoThat could work. Probably it’d be a very easy bit of middleware/express extension to write, it’s basically just wrapping the existing writeHeader, write, end, in the hot new syntax. Probably you’d get some benefits in composability, and lose some performance in the extreme.
- bhouston 2y agoFastify has a model where the request handler takes both a Request and a Reply: https://fastify.dev/docs/latest/Guides/Getting-Started/#your-first-server https://fastify.dev/docs/latest/Guides/Getting-Started/#your...
- silverwind 2y ago`return` statements are inflexible as they prevent doing stuff after them (like logging, which should not delay the response). I prefer express's way.
- throw4950sh06 2y agoExpress way is much better for writing async handlers -streaming, iterators, partial responses, keep-alive, early responses with further processing on background...