Reputation: 10559
The below has given an answer using node.js.
How to close a "Server-Sent Events"-connection on the server?
However, how to do the same thing in python Flask?
Upvotes: 14
Views: 13994
Reputation: 1
A point to note that happened to me is that the GeneratorExit exception is not thrown when you are using flask in a development server and not in production (something to do with wsgi but i don't know why exactly there is this difference). So when you close an event stream from the client side flask will not stop the generator from generating in a development server. Using gunicorn to run the server is working.
Upvotes: 0
Reputation: 6592
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.
Upvotes: 10
Reputation: 759
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.
Upvotes: 1
Reputation: 1483
Well, it depends on the architecture of your app.
Let me show you an example (see this code at https://github.com/jkbr/chat/blob/master/app.py):
def event_stream():
pubsub = red.pubsub()
pubsub.subscribe('chat')
for message in pubsub.listen():
print message
yield 'data: %s\n\n' % message['data']
@app.route('/stream')
def stream():
return flask.Response(event_stream(),
mimetype="text/event-stream")
Flask asks a new message to Redis (locking operation) steadily, but when Flask sees that streaming terminates (StopIteration
, if you aren't new to Python), it returns.
def event_stream():
pubsub = red.pubsub()
pubsub.subscribe('chat')
for message in pubsub.listen():
if i_should_close_the_connection:
break
yield 'data: %s\n\n' % message['data']
@app.route('/stream')
def stream():
return flask.Response(event_stream(),
mimetype="text/event-stream")
Upvotes: 5