Comments (7)
See #106
from cpp-httplib.
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.
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 oftext/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.
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.
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.
Added a SSE example on server.
https://github.com/yhirose/cpp-httplib/blob/master/example/ssesvr.cc
from cpp-httplib.
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)
- How to detect client timeouts/disconnects when implementing long polling HOT 1
- SSL Connection Failed HOT 2
- Segmentation Failed on client side only? HOT 2
- Can't disable CORS policy for POST Server handler HOT 1
- does the community weclome a new blood?/(ㄒoㄒ)/~~ HOT 1
- Server process doesn't get killed when window that is running process gets closed HOT 2
- Client timeout HOT 4
- Crash on httplib post method triggered by malloc.c HOT 8
- What about current version of library ? HOT 1
- When making a form-data request using OkHttp, a status code of 400 is returned. HOT 1
- Server::set_payload_max_length cannot increase the payload size beyond 8 KiB HOT 4
- POST with no POST data returns HTTP 400 HOT 13
- post multipart/form-data errror HOT 1
- usage of rand() is not seeded and depends on seeding by parent program HOT 10
- After using split.py and enable CPPHTTPLIB_ZLIB_SUPPORT, I can't use EventSource. HOT 3
- can't communicate within local network HOT 1
- Doesn't work containerized HOT 1
- A serious problem, crash ... because of request default constructor error!!!!!!! HOT 3
- Already calling select() separately HOT 4
- 【BUG】iOS or macOS http file server not support files larger than 3G HOT 6
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 cpp-httplib.