public
Last active

example use of sse

  • Download Gist
sse example
Python
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
#!/bin/python2.7
# -`*- coding: utf-8 -*-
 
from gevent import monkey
monkey.patch_all()
 
import flask
app = flask.Flask(__name__)
app.debug = True
app.secret_key = 'asdf'
 
import redis
red = redis.Redis()
 
from sse import PeriodicStream, RedisSseStream
import random
 
 
def numGen():
prev = 50
while True:
vals = [prev]
vals.append(prev + random.randrange(-10, 10))
vals.append(prev + random.randrange(-20, 20))
vals.append(prev + random.randrange(-30, 30))
vals.append(prev + random.randrange(-40, 40))
prev = vals[-1]
if prev > 100:
prev = 100
if prev < 0:
prev = 0
for i in vals:
yield i
 
 
numgen = numGen()
numgen2 = numGen()
 
 
def ping(subscriber, freq):
if (subscriber.counter / freq) % 2 == 0:
subscriber.sse.add_message("PING", event='ping')
else:
subscriber.sse.add_message("PONG", event='ping')
 
 
def foo(subscriber, freq):
subscriber.sse.add_message(numgen2.next(), event='graph')
 
app.add_url_rule('/stream',
view_func=PeriodicStream.as_view(str('PeriodicStream'),
{'foo': (1, foo), 'ping': (10, ping)}))
 
 
def data_points(subscriber):
subscriber.sse.add_message(red.get('nextval'), event='graph')
 
app.add_url_rule('/stream2',
view_func=RedisSseStream.as_view(str('RedisSseStream'),
{'graphvals': data_points},
red.pubsub()))
 
 
@app.route('/graph')
def visit():
nextval = numgen.next()
red.set('nextval', nextval)
red.publish('stream', 'graphvals')
return '<!doctype html><head><body>%d</body></html>' % nextval
 
 
@app.route('/')
def home():
return """
<!doctype html>
<head>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js">
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/flot/0.7/jquery.flot.min.js">
</script>
<script>
jQuery( function($) {
var data = [];
function get_data(yval) {
while (data.length != 300) {
data.push(0);
}
data = data.slice(1);
data.push(yval);
// zip the generated y values with the x values
var res = [];
for (var i = 0; i < data.length; ++i) {
res.push([i, data[i]]); }
return res;
}
 
var data2 = [];
function get_data2(yval) {
while (data2.length != 300) {
data2.push(0);
}
data2 = data2.slice(1);
data2.push(yval);
// zip the generated y values with the x values
var res = [];
for (var i = 0; i < data2.length; ++i) {
res.push([i, data2[i]]); }
return res;
}
 
var options = {
series: { shadowSize: 0 },
lines: { show: true, fill: true },
yaxis: { show: false, min: 0, max: 100 },
xaxis: { show: false }
};
 
d1 = get_data2(0);
d2 = get_data(0);
var plot = $.plot( $("#gplot"),
[ { data: d1 }, { data: d2 } ], options
);
 
function update_gplot(yval) {
d2 = get_data(yval);
plot.setData([ { data: d1 }, { data: d2 } ]);
plot.draw();
}
function update_gplot2(yval) {
d1 = get_data2(yval);
plot.setData([ { data: d1 }, { data: d2 } ]);
plot.draw();
}
 
var source = new EventSource('/stream');
source.addEventListener(
'graph', function(e) {
update_gplot(e.data);
}, false
);
source.addEventListener(
'ping', function(e) {
$('#data').text(e.data);
}, false
);
 
var source2 = new EventSource('/stream2');
source2.addEventListener(
'graph', function(e) {
update_gplot2(e.data);
}, false
);
} );
</script>
</head>
<body>
<div id="data">PING</div>
<div id="gplot" style="width:600px;height:300px;"></div>
</body>
</html>
"""
 
if __name__ == '__main__':
app.run()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.