Last active
May 21, 2019 18:26
-
-
Save azharuniverse/28b65239dbe76f2afbe1fdec534c0d4a to your computer and use it in GitHub Desktop.
Coinbasepro fetch ticker from socket with Reconnect Strategy using `ccxt` & `coinbase-pro`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Modify libary file websocket.js from node_modules | |
//this.socket.send(JSON.stringify(message)); -> if(this.socket) this.socket.send(JSON.stringify(message)); | |
/** | |
* Created by Azhar on 21-05-2019. | |
*/ | |
const ccxt = require('ccxt'); | |
let MarketSummaries = new Map(); | |
let tickerCounter = 0; | |
let coinbasepro = new ccxt['coinbasepro']; | |
let websocket; | |
let lastAlive = new Date().getTime(); | |
coinbasepro.fetchMarkets() | |
.then((marketinfo) => { | |
//console.log(market); | |
console.log('coinbasepro fetch market for socket success'); | |
initSocket(marketinfo); | |
}) | |
.catch(err => { | |
//todo handle failure | |
console.error('error in initializing coinbasepro', err); | |
}); | |
function initSocket(marketinfo) { | |
marketids = marketinfo.map(pair => { | |
return pair.id; | |
}); | |
websocket = new (require('coinbase-pro')).WebsocketClient( | |
productIDs = marketids, | |
websocketURI = 'wss://ws-feed.pro.coinbase.com', | |
auth = null, | |
{ channels: ['heartbeat', 'ticker'] } | |
); | |
websocket.on('open', () => { | |
console.log('coinbasepro socket open') // socket connected | |
}); | |
websocket.on('message', data => { | |
/* work with data */ | |
lastAlive = new Date().getTime(); | |
handleTicker(data); | |
}); | |
websocket.on('error', err => { | |
/* handle error */ | |
console.warn('coinbasepro socket error:', err) // socket error | |
}); | |
websocket.on('close', () => { | |
/* ... */ | |
console.error('coinbasepro socket close') // socket closed | |
}); | |
} | |
//Monitor heartbeat and reconnect if needed | |
setInterval(()=> { | |
const timeDiff = ((new Date().getTime() - lastAlive)) / 1000; //Secs | |
if(timeDiff > 10) { //If greater than 10 Secs | |
//reconnect | |
if(websocket.socket) { | |
websocket.socket.close(); | |
websocket.socket = null; | |
} | |
websocket.connect(); | |
console.warn('coinbasepro got disconnected. Trying to reconnect since '+timeDiff+' Secs'); | |
} | |
}, 5000); | |
/** | |
* format - { type: 'ticker', | |
sequence: 8011783, | |
product_id: 'XRP-BTC', | |
price: '0.00007207', | |
open_24h: '0.00006783', | |
volume_24h: '2311434.00000000', | |
low_24h: '0.00006700', | |
high_24h: '0.00008110', | |
volume_30d: '31099622', | |
best_bid: '0.00007203', | |
best_ask: '0.00007214' } | |
*/ | |
function handleTicker(data) { | |
if(!data || data.type !== 'ticker') return; | |
tickerCounter++; | |
if(tickerCounter % 10 === 0) { | |
console.log(`coinbasepro tickers update counter: ${tickerCounter}`); | |
} | |
const dataName = data.product_id.split('-'); | |
let symbol = dataName[0] + '/' + dataName[1]; | |
MarketSummaries[symbol] = { | |
bid: parseFloat(data.best_bid), | |
ask: parseFloat(data.best_ask), | |
last: parseFloat(data.price), | |
timestamp: new Date().toISOString() | |
}; | |
} | |
module.exports = MarketSummaries; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment