yhirose/cpp-httplib

Sending an eventstream

Closed this issue · 4 comments

It looks like cpp-httplib is only able to either send the whole response in one blob of data or use chunked encoding, however is there a way to send an eventstream/"server sent events"?

@niansa there is no official support of SSE. I wrote an experimental implementation in the following files in /example.
This example shows how we can do SSE with cpp-httplib, but it may not fit in with your situation.

https://github.com/yhirose/cpp-httplib/blob/master/example/ssecli.cc
https://github.com/yhirose/cpp-httplib/blob/master/example/ssesvr.cc

Hope it helps!

Thank you very much!

Edit: Doesn't using a chunked_content_provider also send chunk sizes? That would be incorrect for event streams though as they terminate by "\r\n\r\n" and not by length?

Chunking and event streaming operate on different "layers" of response processing and are supposed to be transparent to each other from the perspective of the application. Chunking happens at the HTTP transport/protocol level, while event stream is handled by the application script.
In other words, HTTP client APIs (including browser APIs) should be built such that chunks are "unpacked" internally and whatever script operates on the event messages only sees the events terminated with carriage returns and newlines.

Thanks for the explanation! I understand.