Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow streaming /event/get.

If you omit the "stop" attribute of an /event/get request, new events will
continue to stream in periodically. This allows realtime visualizations of
individual events.
  • Loading branch information...
commit c91c25cef9ca3b11859fd2e304c1b0cae5edc1a4 1 parent 18d2ef3
@mbostock mbostock authored
Showing with 27 additions and 11 deletions.
  1. +27 −11 lib/cube/server/event.js
View
38 lib/cube/server/event.js
@@ -72,11 +72,13 @@ exports.putter = function(db) {
};
exports.getter = function(db) {
- var collection = types(db);
+ var collection = types(db),
+ streamDelay = 5000;
function getter(request, callback) {
- var start = new Date(request.start),
- stop = new Date(request.stop);
+ var stream = !("stop" in request),
+ start = new Date(request.start),
+ stop = stream ? new Date(Date.now() - streamDelay) : new Date(request.stop);
// Validate the dates.
if (isNaN(start)) return util.log("invalid start: " + request.start);
@@ -99,17 +101,31 @@ exports.getter = function(db) {
expression.fields(fields);
// Query for the desired events.
- collection(expression.type).events.find(filter, fields, event_options, function(error, cursor) {
- if (error) throw error;
- cursor.each(function(error, event) {
- if (callback.closed) return cursor.close();
+ function query() {
+ collection(expression.type).events.find(filter, fields, event_options, function(error, cursor) {
if (error) throw error;
- if (event) callback({
- time: event.t,
- data: event.d
+ cursor.each(function(error, event) {
+ if (callback.closed) return cursor.close();
+ if (error) throw error;
+ if (event) callback({
+ time: event.t,
+ data: event.d
+ });
});
});
- });
+ }
+
+ query();
+
+ // While streaming, periodically poll for new results.
+ if (stream) {
+ stream = setInterval(function() {
+ if (callback.closed) return clearInterval(stream);
+ filter.t.$gte = stop;
+ filter.t.$lt = stop = new Date(Date.now() - streamDelay);
+ query();
+ }, streamDelay);
+ }
}
getter.close = function(callback) {
Please sign in to comment.
Something went wrong with that request. Please try again.