diff --git a/app.js b/app.js index e04657e..e48168b 100644 --- a/app.js +++ b/app.js @@ -43,9 +43,18 @@ client = new Primus(server, { client.use('emit', require('primus-emit')); +// Init external API +external = new Primus(server, { + transformer: 'websockets', + pathname: '/external', + parser: 'JSON' +}); + +external.use('emit', require('primus-emit')); + // Init collections var Collection = require('./lib/collection'); -var Nodes = new Collection(); +var Nodes = new Collection(external); Nodes.setChartsCallback(function (err, charts) { diff --git a/lib/collection.js b/lib/collection.js index b9dec8e..2a631c9 100644 --- a/lib/collection.js +++ b/lib/collection.js @@ -2,13 +2,15 @@ var _ = require('lodash'); var Blockchain = require('./history'); var Node = require('./node'); -var Collection = function Collection() +var Collection = function Collection(externalAPI) { this._items = []; this._blockchain = new Blockchain(); this._askedForHistory = false; this._askedForHistoryTime = 0; this._debounced = null; + this._externalAPI = externalAPI; + this._highestBlock = 0; return this; } @@ -76,6 +78,15 @@ Collection.prototype.addBlock = function(id, stats, callback) stats.received = block.block.received; stats.propagation = block.block.propagation; + if(block.block.number > this._highestBlock) + { + this._highestBlock = block.block.number; + this._externalAPI.write({ + action:"bestBlock", + number: this._highestBlock + }); + } + node.setBlock(stats, propagationHistory, callback); } }