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

97 thoughts on “Assign data from websocket to variables”

  1. certainly like your web-site but you have to
    test the spelling on several of your posts. A number of them are rife with spelling issues and I find it very troublesome
    to tell the truth however I will definitely come again again.

    Reply
  2. Greetings from California! I’m bored to death at work so I decided to
    browse your site on my iphone during lunch break. I really
    like the info you provide here and can’t wait to take a look when I get home.
    I’m amazed at how quick your blog loaded on my phone
    .. I’m not even using WIFI, just 3G .. Anyways,
    excellent blog!

    Reply
  3. Hi, I do believe this is an excellent web site. I stumbledupon it 😉
    I will come back yet again since i have saved as a favorite
    it. Money and freedom is the greatest way to change, may you be rich and continue to guide others.

    Reply
  4. It’s the best time to make a few plans for the future and it’s time to
    be happy. I have learn this post and if I may just I want to suggest
    you some interesting things or advice. Perhaps you could write next articles relating to this
    article. I wish to learn more issues about it!

    Reply
  5. Excellent beat ! I would like to apprentice while you amend your site, how
    could i subscribe for a blog site? The account helped me a acceptable deal.

    I had been tiny bit acquainted of this your broadcast offered
    bright clear concept

    Reply
  6. Hey! This is my 1st comment here so I just wanted to give a
    quick shout out and say I truly enjoy reading your articles.

    Can you recommend any other blogs/websites/forums that go
    over the same topics? Thanks a ton!

    Reply
  7. Hello! This is my first comment here so I just wanted to give a
    quick shout out and tell you I really enjoy reading through your blog posts.
    Can you suggest any other blogs/websites/forums that deal with
    the same subjects? Appreciate it!

    Reply

Leave a Comment