Skip to content

EdGENetworks/flask-sse

 
 

Repository files navigation

Flask SSE Build status Test coverage Documentation

A Flask extension for HTML5 server-sent events support, powered by Redis.

Example of sending events:

from flask import Flask
from flask_sse import sse

app = Flask(__name__)
app.config["REDIS_URL"] = "redis://localhost"
app.register_blueprint(sse, url_prefix='/stream')

@app.route('/send')
def send_message():
    sse.publish({"message": "Hello!"}, type='greeting')
    return "Message sent!"

Note- This is optional. If it's given not provided then the connection with the redis with be blocking, to disconnect redis connection after certain time please provide the Redis timeout in flask app config like below:

app.config["SSE_REDIS_TIMEOUT"] = 10 # time unit in seconds

To receive events on a webpage, use Javascript to connect to the event stream, like this:

var source = new EventSource("{{ url_for('sse.stream') }}");
source.addEventListener('greeting', function(event) {
    var data = JSON.parse(event.data);
    // do what you want with this data
}, false);

The full documentation for this project is hosted on ReadTheDocs.

Packages

No packages published

Languages

  • Python 100.0%