├── .gitignore ├── README.md └── app.py /.gitignore: -------------------------------------------------------------------------------- 1 | /lib 2 | /bin 3 | /include 4 | *dump.rdb 5 | *pip-selfcheck.json 6 | *Python 7 | *__pycache__/ 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A primitive chat app created to experiment with Flask, Redis, Gevent & Server-Sent Events. 2 | 3 | ## installation 4 | pip install flask redis gevent gunicorn 5 | 6 | ## running the app 7 | start the redis server 8 | redis-server 9 | gunicorn --debug --worker-class=gevent -t 99999 app:app 10 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import datetime 3 | import flask 4 | import redis 5 | 6 | 7 | app = flask.Flask(__name__) 8 | app.secret_key = 'asdf' 9 | red = redis.StrictRedis() 10 | 11 | 12 | def event_stream(): 13 | pubsub = red.pubsub() 14 | pubsub.subscribe('chat') 15 | # TODO: handle client disconnection. 16 | for message in pubsub.listen(): 17 | print (message) 18 | if message['type']=='message': 19 | yield 'data: %s\n\n' % message['data'].decode('utf-8') 20 | 21 | 22 | @app.route('/login', methods=['GET', 'POST']) 23 | def login(): 24 | if flask.request.method == 'POST': 25 | flask.session['user'] = flask.request.form['user'] 26 | return flask.redirect('/') 27 | return '