-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlevel-1-streaming.js
More file actions
54 lines (46 loc) · 1.38 KB
/
level-1-streaming.js
File metadata and controls
54 lines (46 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const ws = require('ws');
function parseLevel1(message) {
let dataArray = message.split('~');
let dataObj = {
type: dataArray[0],
exchange: dataArray[1],
fromsymbol: dataArray[2],
tosymbol: dataArray[3],
side: dataArray[4] === '1' ? 'bid' : 'ask',
sequence: parseInt(dataArray[5], 10),
price: parseFloat(dataArray[6]),
quantity: parseFloat(dataArray[7]),
timestamp: new Date(parseInt(dataArray[8],10)/1000000)
}
return dataObj;
}
let cryptocompareWS = new ws('wss://streaming.cryptocompare.com');
let subs = {
"action": "SubAdd",
"subs": ["30~bitfinex~BTC~USD"],
"api_key": "YOUR-API-KEY",
"format": "streamer"
}
cryptocompareWS.on('open', function(){
console.log('connection established')
cryptocompareWS.send(JSON.stringify(subs));
});
cryptocompareWS.on('error', function(error){
console.log(error)
});
cryptocompareWS.on('close', function(){
console.log('disconnected');
});
cryptocompareWS.on('message', function(data){
console.log(data)
if (data === '999~HEARTBEAT|') {
console.log(data);
return;
}
let updates = data.split('|');
updates.pop(); //Remove last element as it is always an empty string
for (let update of updates) {
let parsedUpdate = parseLevel1(update);
console.log(JSON.stringify(parsedUpdate))
}
});