Git Product home page Git Product logo

Comments (7)

yhirose avatar yhirose commented on May 17, 2024

See #106

from cpp-httplib.

PixlRainbow avatar PixlRainbow commented on May 17, 2024

As a temporary measure, developers can implement SSE by generating events with their own callback in Response.content_producer because SSE protocol is simply implemented on top of HTTP chunked transfer protocol, which is already supported by cpp-httplib.

For example, check my gist here.

from cpp-httplib.

yhirose avatar yhirose commented on May 17, 2024

Moved the following comment from #206:

SSE in W3 specs is implemented on top of chunked transfer and uses the text/event-stream content type. Groups of events are seperated using a sequence of two line terminators. For the specific case of servers sending a content type of text/event-stream, clients are not expected to interpret double line terminators as end of content. In fact, there is no such thing as "end of content".

However, ContentReceiver is unable to properly handle SSE because it always immediately closes connection when it receives two line terminators.

from cpp-httplib.

PixlRainbow avatar PixlRainbow commented on May 17, 2024

by inspecting Chrome network activity while visiting sites that test SSE capability, I have noticed that some servers which implement SSE will terminate the chunked response after the first chunk (but not the connection) and expect Chrome to send another request using the same connection to resume the event stream.
I suspect that those servers are doing this so that they can "force" web browsers to essentially "ping" the server to show it is still interested in the events.
Since the developers of Chrome appear to be fine with re-sending requests whenever the chunked response is terminated, I think that it should be fine to implement SSE client-side by simply using a loop which re-sends requests on a keep-alive connection, unless the server responds with a non-2xx status, or times out (based on the retry: <milliseconds> directive in SSE messages).
This would require cpp-httplib to allow potentially infinite keep-alive requests, which is not currently supported at the moment.

from cpp-httplib.

PixlRainbow avatar PixlRainbow commented on May 17, 2024

I have updated the github gist with a new demo to show how to make an SSE client.
However, I am not sure if cpp-httplib will re-use connections automatically.

Batching client requests like you currently support is a pretty good idea, but it can't really be used for the cases where requests must be generated on the fly or the number of requests to be made is not known in advance.
If cpp-httplib client currently does not re-use connections automatically, I might suggest to give a downstream developer the ability to manage connection reuse by giving the option to retrieve a "connection" reference/object and pass it to another request as a parameter or otherwise.

from cpp-httplib.

yhirose avatar yhirose commented on May 17, 2024

Added a SSE example on server.
https://github.com/yhirose/cpp-httplib/blob/master/example/ssesvr.cc

from cpp-httplib.

yhirose avatar yhirose commented on May 17, 2024

Added a SSE client example.
https://github.com/yhirose/cpp-httplib/blob/master/example/ssecli.cc

This is the same as curl http://localhost:1234/event1 -N.

from cpp-httplib.

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.