Server Sent Events with cpp-httplib

Hi,

I want to use Server Sent Events (SSE) and dash _extensions.enrich EventSource in my app.
There isan Even tSource example:

and it works. But sse-starlette is the only working configuration I’ve encountered. Dash EventSource does not receive any events sent with flask-sse or cpp-httplib. Though they are working with curl

Also SSE GET request differs between curl-sent and browser-sent messages:

$6 = (const httplib::Request &) @0xffff96fdd9b0: {method = "GET", path = "/v0/midi", headers = std::multimap with 17 elements = {
    ["Accept"] = "text/event-stream", ["Accept-Encoding"] = "gzip, deflate", ["Accept-Language"] = "en-GB,en;q=0.9", ["Cache-Control"] = "no-cache", 
    ["Connection"] = "keep-alive", ["Host"] = "127.0.0.1:9876", ["LOCAL_ADDR"] = "172.20.0.5", ["LOCAL_PORT"] = "9876", ["Origin"] = "http://localhost:8866", 
    ["Pragma"] = "no-cache", ["Referer"] = "http://localhost:8866/", ["REMOTE_ADDR"] = "192.168.65.1", ["REMOTE_PORT"] = "40933", 
    ["Sec-Fetch-Dest"] = "empty", ["Sec-Fetch-Mode"] = "cors", ["Sec-Fetch-Site"] = "cross-site", 
    ["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15"}, body = "",

vs

$7 = (const httplib::Request &) @0xffff96fdd9b0: {method = "GET", path = "/v0/jumbo", headers = std::multimap with 9 elements = {
    ["Accept"] = "text/event-stream", ["Connection"] = "keep-alive", ["Host"] = "localhost:9876", ["Keep-Alive"] = "timeout=60, max=1000", 
    ["LOCAL_ADDR"] = "172.20.0.5", ["LOCAL_PORT"] = "9876", ["REMOTE_ADDR"] = "192.168.65.1", ["REMOTE_PORT"] = "41379", ["User-Agent"] = "curl/8.1.2"}, 
  body = ""

especially in term Accept-Encoding. I suspect there is something unorthodox in sse-starlette internals.

Has anyone managed to handle SSE sent from anything else than sse-starlette in dash?

Any ideas appreciated :slight_smile: Thanks!

In case anyone struggles with similar topic, SSE response requires specific formatting.

  • Despite compression being requested in GET request, Response should not be compressed.
  • Response should start with data:
  • Response should end with 3x \r\n
  • actual data need to follow initial header

data: {} \r\n\r\n\r\n

sse-starlette apparently formats the response that way out-of-the box, for other implementations it probably needs to be double checked.

1 Like