I want to listen for commits to a database with SQLAlchemy and post updates to the browser with Server Sent Events.
I have the below view in a Flask app:
@event.listens_for(scoped_session, 'after_commit')
def event_stream(session):
yield 'data: %s\n\n' % 'helloworld'
@app.route('/stream')
def stream():
return Response(event_stream(scoped_session), mimetype="text/event-stream")
And then simply, in js:
var source = new EventSource('/stream');
source.onmessage = function (event) {
console.log(event);
};
The app is filling the request every 3 seconds, and is disregarding my attempted implementation of the ORM decorator. What am I misunderstanding?