JavaScript -> onmessage can't able to get data

kartik.trade
var qs = getQueryStrings();
var tok1 = qs["request_token"];
var tok2="wss://websocket.kite.trade/?api_key=&user_id=&public_token=";
var tok3=tok2.concat(tok1);
//alert(tok3);
document.getElementById('message1').innerHTML=tok3;
var ws = new WebSocket(tok3);
//ws.binaryType = 'arraybuffer';

ws.onopen = function(ev)
{
document.getElementById('message').innerHTML="Connected to server";
document.getElementById('message1').innerHTML="State" +ws.readyState;
var message = '{"a": "subscribe", "v": [884737]}';
ws.send(JSON.parse(message));
message = '{"a": "mode", "v": ["ltp", [884737]]}';
ws.send(JSON.parse(message));
};
ws.onclose = function(ev)
{
document.getElementById('message').innerHTML="Disconnected";
};
ws.onmessage = function(ev)
{
document.getElementById('message').innerHTML="Receiving...";
var blob=ev.data
document.getElementById('message2').innerHTML=blob.size;
};


I didn't get any size of data in onmessage , please help me to get live quotes.
  • Kailash
    Why don't you check the network inspector to see what's happening to the request?

    PS: You are passing the api_key and user_id, yes?
  • kartik.trade
    Ya I already pass api_key, User_id and public token(access_token).
    But can't able to get data.
  • kartik.trade
    I also check with network inspector but request of websocket.kite.trade.... return me blank cache.
  • Kailash
    @kartik.trade What is the response code from websocket.kite.trade? It should `101 Switching protocols`. You will not get a response body.

    Also, I just noticed that you're doing a JSON.parse() and sending the resultant objects to the API. That is incorrect. You should send the actual JSON string to the API. So `ws.send(message)`
  • kartik.trade
    Ok. I got Data. I pass parse string instead of Json String.

    Do you not think that you should include websocket streaming and postback in "Kite Connect PHP library"?

  • Kailash
    We have it in the Python library. PHP doesn't make a good fit for long running client/server applications, which is why we haven't looked into it.
Sign In or Register to comment.