The SSE interface is not functioning properly

When developing a Server-Sent Events (SSE) service based on the built-in Flask in a Dash application, running it locally allows normal access to the corresponding SSE interface for streaming data return:
demo

However, after the same application is published to Plotly Cloud, the corresponding application and interface access encounter abnormalities:

Due to issues in this regard, it is impossible to deploy LLM applications that rely on streaming responses.


Example application code:

import time
import json
import dash
from dash import html
from flask import Response

app = dash.Dash(__name__)


@app.server.route("/stream")
def stream():
    def _stream():
        for i in range(999):
            time.sleep(1)
            yield "data: {}\n\n".format(json.dumps({"timestamp": time.time()}))

    return Response(_stream(), mimetype="text/event-stream")


app.layout = html.Div(
    ["SSE Example: ", html.A("/stream", href="/stream")], style={"padding": 50}
)

if __name__ == "__main__":
    app.run(debug=True)

Hi @CNFeffery!

Try updating the response header:

response.headers.update({
                "Content-Type": "text/event-stream",
                "Cache-Control": "no-cache",
                "Transfer-Encoding": "chunked",
            })

So the proxy knows how deal with the stream.

I updated the application to the following code, but it still doesn’t work. It might be related to the existing mechanisms of the Plotly Cloud platform:

https://2c55246f-8a50-49b8-87d3-048924564831.plotly.app

import time
import json
import dash
from dash import html
from flask import Response

app = dash.Dash(__name__)


@app.server.route("/stream")
def stream():
    def _stream():
        for i in range(999):
            time.sleep(1)
            yield "data: {}\n\n".format(json.dumps({"timestamp": time.time()}))

    response = Response(_stream(), mimetype="text/event-stream")
    response.headers.update(
        {
            "Content-Type": "text/event-stream",
            "Cache-Control": "no-cache",
            "Transfer-Encoding": "chunked",
        }
    )

    return response


app.layout = html.Div(
    ["SSE Example: ", html.A("/stream", href="/stream")], style={"padding": 50}
)

if __name__ == "__main__":
    app.run(debug=True)

@CNFeffery I’ll have the team take a look!

1 Like

I just found that the sse interface can communicate normally. Thank you very much​:+1:

Currently, in Plotly Cloud, the interfaces such as the one I gave an example of earlier can be accessed normally: Waking up the app...

1 Like