I have made a small Sinatra application to track some of our company data in more or less realtime.
The setup is pretty much like dashing.io/:
- Rufus Scheduler jobs are used to query a database in a certain interval.
- The query result is retued to a SSE coection stream.
- Clients subscribe to the stream (defined in javascript in erb views) and display the data different ways, e.g. using highcharts.
This all works well, however, I've noticed that if I let the job run less frequent than every minute, the client will discoect and recoect all the time. Let me explain:
The subscription javascript code looks like this:
<script type="text/javascript">
var source = new EventSource('/stream/chaels-energy');
source.onopen = function(event) {
console.log("Coection opened", event)
}
source.onerror = function(event) {
console.log("Coection error", event)
}
source.onmessage = function(event) {
var data_energy = JSON.parse(event.data);
console.log("Time: " + event.lastEventId + "Length: " + Object.keys(data_energy).length);
}
</script>
If I let the job run every 30 seconds, everythings works perfectly. A look in the console in Chrome (or Firefox) reveals the following:
Coection opened Event
Time: 2016-03-13T13:33:39.625ZLength: 2854
Time: 2016-03-13T13:34:09.656ZLength: 2854
Time: 2016-03-13T13:34:39.698ZLength: 2854
Time: 2016-03-13T13:35:09.395ZLength: 2854
Time: 2016-03-13T13:35:39.493ZLength: 2854
Time: 2016-03-13T13:36:09.592ZLength: 2854
Time: 2016-03-13T13:36:39.674ZLength: 2854
However, if I adjust the job interval to 3 minutes, the console reveals that the client constantly discoects:
Coection opened Event
Time: 2016-03-13T13:00:16.018ZLength: 2909
Coection error Event
Coection opened Event
Coection error Event
Coection opened Event
Coection error Event
Coection opened Event
Time: 2016-03-13T13:03:15.912ZLength: 2891
Coection error Event
Coection opened Event
Coection error Event
Coection opened Event
Coection error Event
Coection opened Event
Time: 2016-03-13T13:06:15.857ZLength: 2891
Coection error Event
Coection opened Event
Coection error Event
Coection opened Event
Coection error Event
Coection opened Event
...
The client still seem to be able to retrieve the data on input, so the end result doesn't suffer from this. My question is, is this constant discoecting/recoecting normal behaviour? Shouldn't the client just keep the coection open, regardless of how often data gets streamed?
