Skip to content

Instantly share code, notes, and snippets.

@rhyslbw
Forked from roelvan/server.coffee
Last active August 29, 2015 14:17
Show Gist options
  • Save rhyslbw/a5794508ff640f173a54 to your computer and use it in GitHub Desktop.
Save rhyslbw/a5794508ff640f173a54 to your computer and use it in GitHub Desktop.
# on the server: server.coffee
Metrics = (name, aggregationQuery, collection, interval) ->
@name = name
@aggregationQuery = aggregationQuery
@collection = collection
@interval = interval or 1000*10
@_collectionName = 'metrics-transport'
Metrics.prototype._getCollectionName = -> "metrics-#{@name}"
Metrics.prototype._publishCursor = (sub) ->
result = @collection.aggregate @aggregationQuery
sub.added @_collectionName, @name, result: result
sub.ready()
handle = Meteor.setInterval =>
result = @collection.aggregate @aggregationQuery
sub.changed @_collectionName, @name, result: result
, @interval
sub.onStop -> Meteor.clearTimeout handle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment