I am trying to use Websocket for the first time but am not able to get the values of property of event object.
Here's my script
let socket = new WebSocket('wss://stream.binance.com:9443/ws/ethusdt@kline_1m')
let o = {
"method": "SUBSCRIBE",
"params": [
"ethusdt@kline_1m"
],
"id": 1
}
socket.send(JSON.stringify(o))
socket.addEventListener('message', (e)=>{let candle = e.data; console.log(candle['k'])})
e.data returns object like this :
{
"e": "kline", // Event type
"k": {
"o": "0.0010", // Open price
"c": "0.0020", // Close price
"h": "0.0025", // High price
"l": "0.0015", // Low price
}
}
However, my script always returns undefined.
How can I get those values?
Just found that I need to use JSON.parse() to convert e.data into object.
So, it turns to this
socket.addEventListener('message', (e)=>{let candle = JSON.parse(e.data); console.log(candle['k'])})