Skip to content

Instantly share code, notes, and snippets.

@fisadev
Created June 25, 2013 17:59
Show Gist options
  • Select an option

  • Save fisadev/5860754 to your computer and use it in GitHub Desktop.

Select an option

Save fisadev/5860754 to your computer and use it in GitHub Desktop.
flask simple SSE example
# python (flask) side:
import time
from flask import Response
app = Flask(__name__)
@app.route('/event_stream')
def stream():
def event_stream():
while True:
time.sleep(3)
yield 'data: %s\n\n' % 'hola mundo'
return Response(event_stream(), mimetype="text/event-stream")
########################
# html side:
'''
<script>
var source = new EventSource('/event_stream');
source.onmessage = function(event){
alert(event.data);
};
</script>
'''
# if you want to support older browsers, also include this in your html:
# https://github.com/remy/eventsource-h5d/blob/master/public/EventSource.js
@pmourelle

Copy link
Copy Markdown

Muy bueno che. Pasarlo a django es trivial o hay alguna trampa en el medio? (/me se pone la armadura antes de entrar al pasillo)

@fisadev

fisadev commented Jun 25, 2013

Copy link
Copy Markdown
Author

Hmm, no se qué soporte tendrá django para crear event streams, no lo probé.

@fjcapdevila

Copy link
Copy Markdown

Muy bueno. Habrá que probarlo!

@dmoisset

Copy link
Copy Markdown

No es muy distinto en django: https://gist.github.com/dmoisset/5877885

Sabía que se podían poner generator en responses pero la verdad nunca probe con event streams, lindo combo.

@fisadev

fisadev commented Jun 27, 2013

Copy link
Copy Markdown
Author

Genial que en django sea igual de simple :)

@mgaitan

mgaitan commented Jun 30, 2013

Copy link
Copy Markdown

@mgaitan

mgaitan commented Jun 30, 2013

Copy link
Copy Markdown

acá una simple corrección a la versión django, para que funque con middlewares https://gist.github.com/mgaitan/5896086

@mgaitan

mgaitan commented Jun 30, 2013

Copy link
Copy Markdown

btw: muestrenlé esto a cualquier empresa que quiera saber por qué tiene que ayudar a sus empleados a ir a conferencias y eventos como PyCamp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment