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

user94628 picture user94628 · Oct 23, 2012 · Viewed 24.6k times · Source

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