Can flask framework send real-time data from server to client browser?
27,657
You can do so with the help of gevent
+socketio
.
Related videos on Youtube
Author by
user94628
Updated on July 09, 2022Comments
-
user94628 almost 2 years
I was wondering how (if at all) flask performs long polling, so the server can send data over a connection to the client. For example if the server receives a twitter feed via the streaming api how will that be passed to the client browser?
I gather that you cannot use flask.flash for such a situation.
Thanks
Thanks for the examples. I looked at the examples and when I try to implement it for my code, it still does not provide a real-time output in the client browser.
I have based it around the flask snippet() using juggernaut and redis. This is my python code:
import flask from flask.views import MethodView from tweetStreamsRT import StreamerRt from juggernaut import Juggernaut app = flask.Flask(__name__) app.secret_key = "xxxxx" PORT = 8080 class View(MethodView): def get(self): return flask.render_template('index.html') def post(self): results = StreamerRt().filter(track=[flask.request.form['event']]) jug = Juggernaut() jug.publish('channel', results) return self.get() app.add_url_rule('/', view_func = View.as_view('index'), methods=['GET', 'POST']) app.debug = True if __name__ == "__main__": print 'Listening on http://localhost:%s' % PORT app.run()
My html page is, which inherits from a base html page:
{% extends "base.html" %} {% import "forms.html" as forms %} {% block page_header %} <div class="page-header"> <h1>Welcome</h1> </div> {% endblock %} {% block content %} <h2>Enter the Event you would like to follow</h2> <form action="/" method="post"> <input type="text" name="event" /> <input type="submit" value="Submit Query" /> </form> Results: <pre> <script type="text/javascript" charset="utf-8"> var jug = new Juggernaut; jug.subscribe("channel", function(data){ alert("Got data: " + data);}); </script> </pre> {% endblock %}
I'm still confused as to why nothing is sent to the client browser.
Thanks
-
Burhan Khalid over 11 yearsThere is a flask snippet that talks about this very thing.
-
user94628 over 11 yearsThanks Burhan, so if I follow the flask snippet then the inserting those pieces of code in both the client and server. I should be able to publish real time messages to client. Then does that mean that I do not need to use gevent and socketio? Thanks
-
Burhan Khalid over 11 yearsThat's exactly what that means. It will be taken care of for you.
-
Burhan Khalid over 11 yearsYou should answer your question and accept your answer to close it :)
-
K Z over 11 years@user94628 Be aware, Alex MacCaw has stated that Juggernaut has been deprecated: blog.alexmaccaw.com/killing-a-library
-
user94628 over 11 yearsHe says because server sent events do the same ask. But I thought that SSEs don't work with flask. It should still be ok to use juggernaut?
-
K Z over 11 years@user94628 yes, it's mostly the project will not get updated too much down the road. If you are fine with that then go for it.
-
tmthyjames over 9 yearsDid you ever find a solution? I'm having a similar problem.
-
user94628 over 9 years@tmthyjames, it's been a longtime, but I found this to be helpful: flask snippet
http://flask.pocoo.org/snippets/80/
. Ultimately though I decided to useTornado
framework along withwebsockets
to push data to client from the server.
-
-
user94628 over 11 yearsThanks. I'll have a look at the links you gave.