Skip to content

Commit 9a2b95e

Browse files
author
Fabien Penso
committed
We now connect the HighCharts graph to our server through SSE
1 parent 82f245c commit 9a2b95e

7 files changed

Lines changed: 71 additions & 40 deletions

File tree

‎Gemfile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ gem 'sinatra-sse'
1212
gem 'thin'
1313
gem "highcharts-rails"
1414
gem 'jquery-rails'
15+
gem 'jquery-ui-rails'
1516

1617
# Gems used only for assets and not required
1718
# in production environments by default.

‎Gemfile.lock‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ GEM
5353
jquery-rails (2.2.1)
5454
railties (>= 3.0, < 5.0)
5555
thor (>= 0.14, < 2.0)
56+
jquery-ui-rails (4.0.3)
57+
jquery-rails
58+
railties (>= 3.1.0)
5659
json (1.8.0)
5760
libv8 (3.11.8.17)
5861
mail (2.4.4)
@@ -132,6 +135,7 @@ DEPENDENCIES
132135
coffee-rails (~> 3.2.1)
133136
highcharts-rails
134137
jquery-rails
138+
jquery-ui-rails
135139
rails (= 3.2.12)
136140
sass-rails (~> 3.2.3)
137141
sinatra
Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,21 @@
1-
// This is a manifest file that'll be compiled into application.js, which will include all the files
2-
// listed below.
3-
//
4-
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5-
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6-
//
7-
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8-
// the compiled file.
9-
//
10-
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11-
// GO AFTER THE REQUIRES BELOW.
12-
//
131
//= require jquery
142
//= require jquery_ujs
3+
//= require jquery.ui.all
154
//= require_tree .
165
//
176
//= require highcharts
187
//= require highcharts/highcharts-more
198

209
$(function() {
2110
// Create the chart
22-
$('#container').highcharts({
11+
chart = new Highcharts.Chart({
2312
chart: {
13+
renderTo: 'container',
2414
type: 'area',
25-
plotBorderWidth: 0,
26-
events: {
27-
load: function() {
28-
// set up the updating of the chart each second
29-
var series = this.series[0];
30-
setInterval(function() {
31-
var x = (new Date()).getTime(), // current time
32-
33-
// Previous value +/- 10 max
34-
y = Math.round(series.data.slice(-1)[0]['y'] + (Math.random() * 10 * (Math.round(Math.random()) * 2 - 1)));
35-
if (y < 0) {
36-
y = -y
37-
}
38-
series.addPoint([x, y], true, true);
39-
}, 1000);
40-
}
41-
}
15+
plotBorderWidth: 0
4216
},
4317
title: {
44-
text: 'Live random data'
18+
text: 'Live random data received through SSE'
4519
},
4620
xAxis: {
4721
type: 'datetime'
@@ -71,14 +45,45 @@ $(function() {
7145
name: 'Random data',
7246
lineWidth: 2,
7347
data: (function() {
74-
// generate an array of random data
75-
var data = [], time = (new Date()).getTime(), i;
76-
var previous;
77-
for( i = -60; i <= 0; i+=1) {
78-
data.push([time + i * 1000, Math.round(Math.random() * 100)]);
48+
// Initializing points 20 points to 0
49+
var datapoints = [], time = (new Date()).getTime(), i=-20;
50+
while(i <= 0) {
51+
// time is milliseconds, we display the last 20 sec points
52+
datapoints.push([(time + (i*1000)), 0]);
53+
i += 1;
7954
}
80-
return data;
55+
return datapoints;
8156
})()
8257
}]
8358
});
59+
60+
if (!!window.EventSource) {
61+
var source = new EventSource('/realtime-analytics');
62+
var date = new Date();
63+
64+
// counter matches what you send in lib/realtime_analytics.rb
65+
source.addEventListener('FOOBAR', function(e) {
66+
x = (new Date().getTime());
67+
y = parseFloat(e.data)
68+
chart.series[0].addPoint([x, y], true, true);
69+
$("#last_received").html(y);
70+
$("#last_received").effect( "highlight", {}, 500 );
71+
}, false);
72+
73+
source.addEventListener('message', function(e) {
74+
console.log(e.data);
75+
}, false);
76+
77+
source.addEventListener('open', function(e) {
78+
console.log("SSE connection opened");
79+
}, false);
80+
81+
source.addEventListener('error', function(e) {
82+
if (e.readyState == EventSource.CLOSED) {
83+
console.log("SSE connection closed");
84+
}
85+
}, false);
86+
} else {
87+
console.log("SSE not supported by your browser");
88+
}
8489
});
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
// Place all the styles related to the analytics controller here.
2-
// They will automatically be included in application.css.
3-
// You can use Sass (SCSS) here: http://sass-lang.com/
1+
body {
2+
font-family: "Times New Roman",Georgia,Serif;
3+
}
44
#container {
55
width: 500px;
66
margin: auto;
77
height: 300px;
88
min-width: 300px;
99
}
10+
#last_received_div {
11+
margin: auto;
12+
width: 500px;
13+
text-align: center;
14+
padding-top: 2em;
15+
}

‎app/views/analytics/index.html.erb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11

22
<div id="container"></div>
33

4+
<div id="last_received_div">Last data received: <span id="last_received">-</span></div>

‎config/routes.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'realtime_analytics'
12
BlogSse::Application.routes.draw do
3+
mount AnalyticsSSE => '/realtime-analytics'
24
root to: 'analytics#index'
35
end

‎lib/realtime_analytics.rb‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class AnalyticsSSE < Sinatra::Base
2+
include Sinatra::SSE
3+
get '/' do
4+
sse_stream do |out|
5+
number = 0
6+
EM.add_periodic_timer(2) do
7+
number = 10 + rand(50)
8+
out.push :event => 'FOOBAR', :data => number.to_s
9+
end
10+
end
11+
end
12+
end

0 commit comments

Comments
 (0)