Skip to content

Instantly share code, notes, and snippets.

@icehongssii
Created August 16, 2018 02:22
Show Gist options
  • Save icehongssii/2dcad130d595c0c1524722d4ce4306c1 to your computer and use it in GitHub Desktop.
Save icehongssii/2dcad130d595c0c1524722d4ce4306c1 to your computer and use it in GitHub Desktop.
Getting last price from cexio
const app = require('express')();
const server = require('http').Server(app);
const request = require('request');
const Websocket = require('ws');
function getPairs(){
return new Promise((resolve, reject)=>{
request('https://cex.io/api/currency_limits', function(error, res, body){
if (res.statusCode == 200 && !error){
var pairs = []
var result = JSON.parse(body).data.pairs
//console.log(result[0].symbol1);
var i;
for(i=0; i<result.length; i++){
pairs.push("pair-"+result[i].symbol1+"-"+result[i].symbol2)
}
resolve(pairs)
}
});
});
}
async function queryMaker(){
var pairs = await getPairs()
var sub_dict = {'e': 'subscribe','rooms':["tickers"]}
sub_dict['rooms'].push.apply(sub_dict['rooms'], pairs)
const URL = "wss://ws.cex.io/ws"
const ws = new Websocket(URL);
ws.on('open', function open(){
ws.send(JSON.stringify(sub_dict))
});
ws.on('message', function incoming(data){
var result = JSON.parse(data);
if ('tick' == (result.e)){
var price = result.data.price
var pair = result.data.symbol1 + result.data.symbol2
console.log('price_data', {pair, price});
}
});
}
server.listen(3005);
app.get('/', function (req, res, err) {
res.sendFile('/index.html',{"root":__dirname});
if (err) {
console.log(err)
}
});
queryMaker()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment