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

9
  • There is a flask snippet that talks about this very thing. Commented Oct 23, 2012 at 17:03
  • 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? Thanks Commented Oct 23, 2012 at 17:29
  • That's exactly what that means. It will be taken care of for you. Commented Oct 23, 2012 at 17:31
  • You should answer your question and accept your answer to close it :) Commented Oct 23, 2012 at 17:35
  • 1
    @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. Commented Dec 14, 2014 at 17:55

1 Answer 1

25

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I'll have a look at the links you gave.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.