diff --git a/app/models/Transaction.js b/app/models/Transaction.js index 57d2fc29..33ceb125 100644 --- a/app/models/Transaction.js +++ b/app/models/Transaction.js @@ -91,18 +91,20 @@ TransactionSchema.statics.fromIdWithInfo = function(txid, cb) { TransactionSchema.statics.createFromArray = function(txs, next) { var that = this; if (!txs) return next(); - + var mongo_txs = []; async.forEach( txs, function(tx, callback) { - that.create({ txid: tx }, function(err) { + that.create({ txid: tx }, function(err, new_tx) { if (err && ! err.toString().match(/E11000/)) { return callback(err); } + console.log(new_tx); + mongo_txs.push(new_tx); return callback(); }); }, function(err) { - return next(err); + return next(err, mongo_txs); } ); }; diff --git a/app/views/sockets/main.js b/app/views/sockets/main.js index 9f7ce409..ce01b69a 100644 --- a/app/views/sockets/main.js +++ b/app/views/sockets/main.js @@ -3,7 +3,11 @@ var Transaction = require('../../models/Transaction'); // server-side socket behaviour -module.exports = function(app, io) { + +var io = null; + +module.exports.init = function(app, io_ext) { + io = io_ext; io.set('log level', 1); // reduce logging io.sockets.on('connection', function(socket) { Transaction.findOne(function(err, tx) { @@ -14,3 +18,7 @@ module.exports = function(app, io) { }); }; + +module.exports.broadcast_tx = function(tx) { + io.sockets.emit('tx', tx); +}; diff --git a/lib/Sync.js b/lib/Sync.js index 6b4fe0c9..3bbca8d4 100644 --- a/lib/Sync.js +++ b/lib/Sync.js @@ -13,6 +13,7 @@ function spec() { var Block = require('../app/models/Block'); var Transaction = require('../app/models/Transaction'); var TransactionItem = require('../app/models/TransactionItem'); + var sockets = require('../app/views/sockets/main.js'); function Sync(config) { this.tx_count =0; @@ -61,11 +62,13 @@ function spec() { Sync.prototype.storeTxs = function(txids, cb) { var that=this; - Transaction.createFromArray(txids, function(err) { + Transaction.createFromArray(txids, function(err, inserted_txs) { if (err) return cb(err); - async.each(txids, function(txid, next) { + async.each(inserted_txs, function(new_tx, next) { + var txid = new_tx.txid; + sockets.broadcast_tx(new_tx); // This will trigger an RPC call Transaction.explodeTransactionItems( txid, function(err) { that.tx_count++; @@ -202,6 +205,7 @@ function spec() { if (!(opts && opts.skip_db_connection)) { mongoose.connect(config.db); } + this.opts = opts; this.db = mongoose.connection; this.rpc = new RpcClient(config.bitcoind); diff --git a/public/js/controllers/index.js b/public/js/controllers/index.js index 3d30446f..06d4b7d7 100755 --- a/public/js/controllers/index.js +++ b/public/js/controllers/index.js @@ -5,7 +5,7 @@ angular.module('mystery.system').controller('IndexController', ['$scope', 'Globa socket.on('tx', function(data) { var tx = data; console.log('Transaction received! ' + tx.txid); - $scope.txs.push(tx.txid); + $scope.txs.unshift(tx.txid); }); $scope.txs = []; diff --git a/server.js b/server.js index f1fc7183..fa7f5f85 100644 --- a/server.js +++ b/server.js @@ -59,7 +59,7 @@ require('./config/routes')(app); // socket.io var server = require('http').createServer(app); var io = require('socket.io').listen(server); -require('./app/views/sockets/main.js')(app,io); +require('./app/views/sockets/main.js').init(app,io); //Start the app by listening on var port = process.env.PORT || config.port;