Assign data from websocket to variables

I’m getting data from a website via a websocket stream, I can see it clearly as a Json object in the console, and now I would like to display it in my frontend but I’m not sure how to do it. I tried assigning the data to a variable but I struggle a little bit to dot so. Is that even the solution ?

my code that fetches the data goes as follow :

const ws = new WebSocket('wss://ws-feed.pro.coinbase.com');

ws.onopen = function () {
  ws.send(JSON.stringify({
    "type": "subscribe",
    "product_ids": [
      "BTC-USD",
    ],
    "channels": [
      "level2"
    ]
  }));
};

ws.onmessage = function (message) {
 const msg = JSON.parse(message.data);
  console.log(msg)
}

Even if I manage to display it in my app, it would appear as a whole and I would like to split each key/value pair of the object into different variables to style it properly, how can I do that ?

Thank you very much

6 thoughts on “Assign data from websocket to variables”

Leave a Comment