Comments (7)
Would be great to have! We currently rely on timeouts / a special chunk indicating end of messages.
from plug.
We do the exact same thing: collect a SSE stream and recombine it into a full response for caching. In our particular use case, we know in advance the SSE streams are not long-lived and so we can either rely on a timeout or the protocol specifying a sentinel end-of-stream message.
I found https://www.rfc-editor.org/rfc/rfc6202 quite helpful, specifically:
Each chunk
starts with the hexadecimal expression of the length of its data,
followed by CR/LF (the end of the response is indicated with a chunk
of size 0).
answers my question about the terminating chunk for HTTP/1.1 streaming
To achieve the same result, an HTTP/1.0 server will omit the Content-
Length header in the response. Thus, it will be able to send the
subsequent parts of the response on the same connection (in this
case, the different parts of the response are not explicitly
separated by HTTP protocol, and the end of the response is achieved
by closing the connection).
I could be wrong, but I believe this is what we rely on in our implementation.
from plug.
I have reverted register_before_chunk for now, in case we need to modify it so it considers closing as well.
from plug.
I am not sure this is possible because we don't have a callback that notifies the chunk is done. So you would need to provide such a mechanism yourself. You can store a callback in assigns and invoke it when you are done.
from plug.
I'm curious how @feynmanliang intends to use Plug.Conn.register_before_chunk/2
. Do you have the same need to figure out when the last chunk is sent? If so, how do you achieve it?
I am not sure this is possible because we don't have a callback that notifies the chunk is done.
Indeed, and I don't understand how plug notifies the adapter streaming is over 🤔 Cowboy's adapter for instance expects a fin
parameter to be passed to the cowboy_req:stream_body/3
function to send the terminating "0/r/n/r/n"
of a chunk-encoded response.
from plug.
@tanguilp do you mind sharing where you found that "0/r/n/r/n"
should terminate a SSE stream? I am reading the WHATWG spec and can't seem to locate it.
from plug.
@feynmanliang It was not clear in my message but I was talking about chunked transfer encoding, not SSE events.
My use cache is HTTP caching. I want to cache chunks these responses by chunk and recombine the chunks afterwards. To do this I need to know when the last chunk is sent (otherwise there's no way there won't be an additional chunk). I'm curious what you use case is.
from plug.
Related Issues (20)
- [Bug/compile] can't compile plug in bigger project, pop-os-22.04 HOT 9
- Plug.Conn.Query - decode_init, each,done unknown HOT 7
- Interest in an extension for Plug.Parsers.MULTIPART for FormData? HOT 2
- Mismatch spec for `Plug.Conn.Query.decode` HOT 1
- Plug.RequestId: put the `request_id` in `:private` or `:assigns`? HOT 1
- 1.15 changes decoding behaviour of `[]` HOT 5
- Request: Support MFA tuple for Plug.Session opts HOT 4
- Plug.Upload's random-tempdir generation is not very random HOT 1
- `strip_spaces` fails on `nil` since 1.15.0 HOT 1
- Plug.Conn.Query has changed the way it decodes parameters from 1.14 to 1.15 without warning HOT 1
- More flexible dynamic routes with `:` parameters HOT 2
- Compatibility issue with plug_crypto 2.0.0 HOT 3
- Error: There isn't enough space to open the popup HOT 1
- Docs inconsistency between Plug moduledoc and Plug.run HOT 1
- Plug.Upload: demonitor previous owner when giving away HOT 1
- Compilation issues on Erlang 26.x HOT 3
- html array items are dropped. HOT 1
- Plug.Conn is not closed when user closes the browser's tab (SSE) HOT 2
- setting max_age to nil in put_resp_cookie/4 causes crash HOT 1
- Sending a zip created on-the-fly via Plug.Conn.chunk fails after around 200Mb HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from plug.