Im trying to get the flask-sse example code to work on my raspberry pi 3. The website loads, but I get the message in the index.html in line 15 Failed to connect to event stream. Is Redis running?
Redis is running and after trying multiple times I checked on it with redis-cli monitor
it shows activity.
redis-cli monitor (shortened):
1676659598.612265 [0 [::1]:58788] "SUBSCRIBE" "sse"
1676659616.141010 [0 [::1]:39662] "SUBSCRIBE" "sse"
1676659624.109693 [0 [::1]:54564] "SUBSCRIBE" "sse"
1676659627.603954 [0 [::1]:54578] "PUBLISH" "sse" "{\"data\": {\"message\": \"Hello!\"}, \"type\": \"greeting\"}"
1676659627.614501 [0 [::1]:58788] "UNSUBSCRIBE" "sse"
1676659627.621229 [0 [::1]:39662] "UNSUBSCRIBE" "sse"
1676659651.509510 [0 [::1]:53316] "SUBSCRIBE" "sse"
(i was reloading the pages so that's why it happend multiple times, but none of them showed results)
to start the server:
$ sudo systemctl start nginx
$ redis-server --daemonize yes
$ gunicorn sse:app --worker-class gevent --workers=3 --bind 127.0.0.1:8000 --daemon
the python code:
from flask import Flask, render_template
from flask_sse import sse
app = Flask(__name__)
app.config["REDIS_URL"] = "redis://localhost"
app.register_blueprint(sse, url_prefix='/stream')
@app.route('/')
def index():
return render_template("index.html")
@app.route('/hello')
def publish_hello():
sse.publish({"message": "Hello!"}, type='greeting')
return "Message sent!"
index.html:
<!DOCTYPE html>
<html>
<head>
<title>Flask-SSE Quickstart</title>
</head>
<body>
<h1>Flask-SSE Quickstart</h1>
<script>
var source = new EventSource("{{ url_for('sse.stream') }}");
source.addEventListener('greeting', function(event) {
var data = JSON.parse(event.data);
alert("The server says " + data.message);
}, false);
source.addEventListener('error', function(event) {
alert("Failed to connect to event stream. Is Redis running?");
}, false);
</script>
</body>
</html>