How do I close a Server-Send Events connection in Flask?

Viewed 9672
3 Answers

It is important to note you need to close the connection on the client as well, otherwise it will try and reopen the connection after a retry timeout.

This was confusing me until I saw this post here: https://stackoverflow.com/a/38235218/5180047

From the link:

The problem here is that the server unexpectedly closes the connection, instead of doing its work while leaving it open. When this happnes, the client re-sends a request to open a connection and to start streaming Server Sent Events. The server then closes the connection again and again, leading to an infinite loop.

After I saw this.. my preferred solution was to close the connection from the server by yielding an expected data message.

On the client (browser)

var sse = new EventSource('/sse');
sse.addEventListener('message', function(e) {
  console.log(e);
  var data = e.data;
  if (!data) {
      console.log('no data in event');
      return;
  }
  if (data === 'finished') {
      console.log('closing connection')
      sse.close()
  }
  // my work here based on message
});

My Flask Server

from flask import Response

@app_api.route("/sse")
def stream():
    def trackerStream():
        finished = check_if_finished()
        while not finished:
            sleep(1) # poll timeout
            if should_send_event():
                yield f"data: {mydata()}\n\n"
            else:
                yield "data: nodata\n\n"

            finished = check_if_finished()

        yield "data: finished\n\n"

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

Note: Make sure you're always sending events at some interval from the server to the client. If the user closes the browser, flask will get an error while trying to write to the socket and will close the stream for you. If you aren't writing to the client at some interval, even if you're just writing data: nodata\n\n, then the server could get stuck in a loop.

I had the same problem and finally found the following solution here.

import time
from flask import Flask, Response, stream_with_context

app = Flask(__name__)

@app.route('/stream')
def stream():
    def gen():
        try:
            i = 0
            while True:
                data = 'this is line {}'.format(i)
                print(data)
                yield data + '<br>'
                i += 1
                time.sleep(1)
        except GeneratorExit:
            print('closed')

    return Response(stream_with_context(gen()))

You can start the method with subscribing a listener to Redis. Instead of time.sleep(1) you could wait for Redis' listen() method to return a vlaue. Instead of print('closed') you could unsubscribe from Redis. The only problem left is that the GeneratorExit exception is only raised when the yield value is being sent to the client. So if Redis' listen() never ends, then you will never discover that the connection has been broken.

Related