-
Notifications
You must be signed in to change notification settings - Fork 10
/
sample.py
33 lines (28 loc) · 825 Bytes
/
sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import flask
from datetime import datetime
from sse import Publisher
app = flask.Flask(__name__)
publisher = Publisher()
@app.route('/subscribe')
def subscribe():
return flask.Response(publisher.subscribe(),
content_type='text/event-stream')
@app.route('/')
def root():
ip = flask.request.remote_addr
publisher.publish('New visit from {} at {}!'.format(ip, datetime.now()))
return """
<html>
<body>
Open this page in new tabs to see the real time visits.
<div id="events" />
<script>
var eventSource = new EventSource('/subscribe');
eventSource.onmessage = function(e) {
document.getElementById('events').innerHTML += e.data + '<br>';
}
</script>
</body>
</html>
"""
app.run(debug=True, threaded=True)