0

i am trying to get data from Binance web sockets in my react app

const ws = new WebSocket("wss://stream.binance.com:9443/ws/ethbtc@trade");
ws.onopen = () => {
  ws.send(
    JSON.stringify({
      method: "SUBSCRIBE",
      params: ["ethbtc@trade"],
      id: 13
    })
  );
};
ws.onmessage = evnt => {
  console.log(evnt.data);
};

and the response is {"e":"trade","E":1593865856744,"s":"ETHBTC","t":180904813,"p":"0.02493100","q":"0.90600000","b":788853315,"a":788853661,"T":1593865856743,"m":true,"M":true}

but when trying to get a specific value from the above object it shows undefined !

console.log(evnt.data.s)

it shows undefined

if anyone can help it will be great, Thanks in advance!

4
  • 1
    What's the result of console.log(typeof event.data)? Commented Jul 4, 2020 at 12:41
  • 1
    maybe the response data is String. If it is string, you need to parse before accessing it Commented Jul 4, 2020 at 12:42
  • @Mitya It is showing String ! Commented Jul 4, 2020 at 12:43
  • 1
    OK so your data isn't an object - needs parsing. Commented Jul 4, 2020 at 12:53

2 Answers 2

1

please try like this

  1. console.log(evnt.data["s"]) Or,
  2. console.log(JSON.parse(evnt.data).s) Or,
  3. console.log(evnt.data.toObject().s)
Sign up to request clarification or add additional context in comments.

5 Comments

Thank You console.log(JSON.parse(evnt.data).s) the second one works for me !
It's my pleasure. can you please knock me here? link: facebook.com/talha.aiubian
i stopped using fb :-(..no more account !
How can I contact with you?
linkedin.com/in/aashiqotp/ or instagram : @Aashiq_Otp
1

try this may be it solves your problem

JSON.parse(event.data).s 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.