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

user94628
  • 3,641
  • 17
  • 51
  • 88
  • There is a [flask snippet](http://flask.pocoo.org/snippets/80/) that talks about this very thing. – Burhan Khalid Oct 23 '12 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 – user94628 Oct 23 '12 at 17:29
  • That's exactly what that means. It will be taken care of for you. – Burhan Khalid Oct 23 '12 at 17:31
  • You should answer your question and accept your answer to close it :) – Burhan Khalid Oct 23 '12 at 17:35
  • @user94628 Be aware, Alex MacCaw has stated that Juggernaut has been deprecated: http://blog.alexmaccaw.com/killing-a-library – K Z Oct 23 '12 at 21:09
  • 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? – user94628 Oct 23 '12 at 22:11
  • @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. – K Z Oct 24 '12 at 08:54
  • Did you ever find a solution? I'm having a similar problem. – tmthyjames Dec 14 '14 at 17:47
  • 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. – user94628 Dec 14 '14 at 17:55

1 Answers1

25

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

K Z
  • 29,661
  • 8
  • 73
  • 78