Skip to content

Instantly share code, notes, and snippets.

@christiannwamba
Created May 30, 2017 11:42
Show Gist options
  • Save christiannwamba/a40d1dd4fe818f887aada2d8258a66b9 to your computer and use it in GitHub Desktop.
Save christiannwamba/a40d1dd4fe818f887aada2d8258a66b9 to your computer and use it in GitHub Desktop.
var ds = require('deepstream.io-client-js')
var data = require('./data.json');
var client = ds('APP-URL').login()
client.rpc.provide( 'price-chart', ( request, response ) => {
console.log(data);
response.send( formattedData( data.bpi ) );
});
var curPrice = client.record.getRecord('BTC/price');
curPrice.whenReady((record) => {
setInterval(() => {
var price = 1200 * (0.99 + Math.random() / 10)
console.log('setting...')
record.set('price', price, (err) => {
if (err) {
console.log(err)
return
}
console.log('Set price to', price)
})
}, getRandom(1000, 3000))
})
function getRandom(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function formattedData(data) {
var nArray = [];
var iArray = Object.keys(data);
iArray.forEach(x => {
var nObject = {y: data[x]}
nArray.push(nObject)
})
return nArray;
}
console.log('Running')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment