const WebSocket = require('ws')
let socket = new WebSocket('wss://ftx.com/ws/');
const dataObject = {'op': 'subscribe', 'channel': 'trades', 'market': 'BTC-PERP'};
socket.onopen = function() {
// Send an initial message
socket.send(JSON.stringify(dataObject));
console.log('Connected')
messages = []
socket.onmessage = (msg) => {
const priceData = JSON.parse(msg.data)
messages.push(priceData.data)
console.log(priceData.data)
}
// Listen for socket closes
socket.onclose = function(event) {
console.log('Client notified socket has closed', event);
};
// To close the socket....
// socket.close()
};
[nodemon] starting `node socket-ftx.js`
Connected
undefined
[
{
id: 5014962233,
price: 19280,
size: 0.03,
side: 'sell',
liquidation: false,
time: '2022-09-22T20:24:52.011309+00:00'
},
{
id: 5014962234,
price: 19280,
size: 0.02,
side: 'sell',
liquidation: false,
time: '2022-09-22T20:24:52.011309+00:00'
},
{
id: 5014962235,
price: 19280,
size: 0.0047,
side: 'sell',
liquidation: false,
time: '2022-09-22T20:24:52.011309+00:00'
}
]
I am trying to store the price and size elements into an array, however I can't parse that array from the response.
priceData.data[0].price does not work. It results in a TypeError. I have tested (typeof passed priceData.data) and it logs as an object and not an array. I am confused here...