Git Product home page Git Product logo

Comments (7)

feynmanliang avatar feynmanliang commented on June 27, 2024 1

Would be great to have! We currently rely on timeouts / a special chunk indicating end of messages.

from plug.

feynmanliang avatar feynmanliang commented on June 27, 2024 1

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.

josevalim avatar josevalim commented on June 27, 2024 1

I have reverted register_before_chunk for now, in case we need to modify it so it considers closing as well.

from plug.

josevalim avatar josevalim commented on June 27, 2024

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.

tanguilp avatar tanguilp commented on June 27, 2024

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.

feynmanliang avatar feynmanliang commented on June 27, 2024

@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.

tanguilp avatar tanguilp commented on June 27, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.