-3
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...

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Where did you try to access any element from the array? What was the exact error message and which line of code produced that error? – David Sep 22 '22 at 20:35
  • typeof someArray is going to be "object". That is normal. (It's still an array). You could use Array.isArray(someArray) to verify. – James Sep 22 '22 at 20:37
  • Array.isArray(priceData.data) was false – Blake D'Ippolito Sep 22 '22 at 20:46
  • What does `console.log(priceData)` show when this happens? – Barmar Sep 22 '22 at 20:52
  • Seems odd that it displays `undefined` right after `Connected` there. Are you sure `priceData.data` is defined? Also, I'm not sure that "destructure" is the correct term to be using, considering there is no evidence of destructuring here. If you had `const { data } = JSON.parse(msg.data);` then we could talk about destructuring. – Heretic Monkey Sep 22 '22 at 20:59
  • You are right @HereticMonkey. I am obviously a noob here lol. I am grabbing the .data from the ftx websocket api. It is not defined in my code – Blake D'Ippolito Sep 22 '22 at 21:11

1 Answers1

0

The problem is likely that the websocket sometimes send you message that are not priceData arrays. You need to add a check before manipulating it as an array.

socket.onopen = function() {

    // Send an initial message
    socket.send(JSON.stringify(dataObject));
    console.log('Connected')

    messages = []

    socket.onmessage = (msg) => {
        // You could start by adding a log on the message, it might help you debug the issue
        // [EDITED OUT as msg is an Event and JSON.stringify will just return an empty object] console.log(`Message value: ${JSON.stringify(msg)}`);
         console.log(`Message value: ${msg.data}`);
        const priceData = JSON.parse(msg.data)
        
        if (Array.isArray(priceData.data) /* you might add an additional check if the msg contains an identifier of the data type */) {
          // Do your array manipulations here
        } else {
          // it's not a priceData array, do something else if needed
        }
    }



    // Listen for socket closes
    socket.onclose = function(event) {
        console.log('Client notified socket has closed', event);
    };

    // To close the socket....
    // socket.close()

};
user3252327
  • 617
  • 5
  • 9
  • `msg` or `priceData` probably has another field that indicates what kind of message it is. – Barmar Sep 22 '22 at 20:48
  • Oh well msg is actually an event. Could you try to `console.log(msg.data)` instead of `console.log(JSON.stringify(msg))`? – user3252327 Sep 22 '22 at 20:59