Can flask framework send real-time data from server to client browser?
Asked Answered
C

1

22

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

Claymore answered 23/10, 2012 at 12:17 Comment(9)
There is a flask snippet that talks about this very thing.Henslowe
Thanks 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? ThanksClaymore
That's exactly what that means. It will be taken care of for you.Henslowe
You should answer your question and accept your answer to close it :)Henslowe
@Claymore Be aware, Alex MacCaw has stated that Juggernaut has been deprecated: blog.alexmaccaw.com/killing-a-libraryAmundson
He 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?Claymore
@Claymore 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.Amundson
Did you ever find a solution? I'm having a similar problem.Armorer
@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 use Tornado framework along with websockets to push data to client from the server.Claymore
A
25

You can do so with the help of gevent+socketio.

Amundson answered 23/10, 2012 at 12:30 Comment(1)
Thanks. I'll have a look at the links you gave.Claymore

© 2022 - 2024 — McMap. All rights reserved.