using websocket for Live data

KrishnakantB
Hello All,
I am using PHP with javascript to get live data from zerodha. I found below code from documentation here https://kite.trade/docs/connect/v3/websocket/ but I did not get any proper response.



var ticker = new KiteTicker({api_key: "api_key", access_token: "<? $user->access_token;?>"});

ticker.connect();
ticker.on("ticks", onTicks);
ticker.on("connect", subscribe);

function onTicks(ticks) {
console.log("Ticks", ticks);
}

function subscribe() {
var items = [408065];
ticker.subscribe(items);
ticker.setMode(ticker.modeFull, items);
}

I got Unexpected response code: 400 from the server. Can anybody guide how to get the live trading data using WebSocket in PHP by resolving this error or any other way out.
Thanks in advance.
  • sujith
    Can you paste the complete stacktrace here?
    Are you sure you are sending the tokens properly?
  • sujith
    sujith edited March 9
    We don't have WebSockets API library for PHP. You will have to use kiteconnectjs for Websockets API.
  • KrishnakantB
    yes I am sending the token properly, stacktrace :

    ticker.js:263 WebSocket connection to 'wss://ws.kite.trade/?api_key=*********************&access_token=&uid=************' failed: Error during WebSocket handshake: Unexpected response code: 400
  • KrishnakantB
    how/where can i find detail of kiteconnect.js
  • sujith
    You can check out here.
  • sujith
    Try running in debug mode and check logs.
  • KrishnakantB
    Hi Sujith, the kiteconnectjs is in nodejs, right? So should we consider that we can not fetch the live data via PHP ? If there is any way out then let us know. And if we use this kiteconnectjs node js then how will be the integration with PHP in that case? will it work ? please guide. Also - in the link which you sent - I saw below line :
    var KiteTicker = require("kiteconnect").KiteTicker; We installed the module by npm install [email protected] command but not able to find the reference of this 'require'. Please share something about it as well.
  • sujith
    We don't have Websocket solution in PHP. You will have to use it with the kiteconnectjs for live streaming data.
    Can you print the kiteconnect version you have installed and let us know?
  • KrishnakantB
    So do you mean that we can not build the system in PHP for Zerodha APIs ?
  • KrishnakantB
    Here is the version -

    "kiteconnect": {
    "version": "3.0.0-beta.3",
    "resolved": "https://registry.npmjs.org/kiteconnect/-/kiteconnect-3.0.0-beta.3.tgz",
    "integrity": "sha512-70aXvZy1maAXDcaNI2BKe2Ew3vhdJ/CN1O2BFzuZNkigPvZMjJMCIvmQx4YRYpa2A3xhPCNlejLFxh0l5CPNyA==",
    "requires": {
    "axios": "^0.17.1",
    "crypto-js": "^3.1.9-1",
    "papaparse": "^4.3.6",
    "ws": "^1.1.1"
    }

    "version": "3.0.0-beta.3",
  • sujith
    You can, it is just that you will have to write the Websocket implementation on your own or use the kiteconnectjs for that.

    We didn't do it because the philosophy of the PHP doesn't align with something like a Websocket. I think in recent versions of PHP, the Websocket support was added.

    You need to use the 3.1.0, use the master channel no need to use the beta version since it is old.
  • KrishnakantB
    KrishnakantB edited March 11
    If we use kiteconnectjs for live data then how to integrate it with the other PHP system we have developed/developing ? will it support? Not sure so asking this.
    Also - about the version - you told us to install the beta version so installed that. If you would have told 3.1.0 earlier, it could have saved our time. No problem, will check it now.
  • sujith
    I am not sure maybe your PHP code will invoke the nodejs version for websocket data and you can publish an event from the on_tick of the nodejs to trigger some PHP code or you may dump all the data in one place and let the PHP code pick it up. It depends on your use case.

    We will update the documentation to use npm install kiteconnect
  • KrishnakantB
    So should we consider that it can't be done in PHP alone? we have to see how to use the nodejs for websocket and then invoke in PHP code. Seems quite tough for someone who knows PHP but not nodejs. We picked up PHP considering the expertise and experience we have and now stuck up to move further !
  • KrishnakantB
    Also - we installed beta version, can you please confirm how to install the 3.1.0 version? Is there any support available on call wherein we can discuss and resolve the queries/doubts quickly?
  • sujith
    Since there are no major breaking changes, we are directly going live with the master. You can use npm install kiteconnect
  • KrishnakantB
    So should we consider that it can't be done in PHP alone? we have to see how to use the nodejs for websocket and then invoke in PHP code. Seems quite tough for someone who knows PHP but not nodejs. We picked up PHP considering the expertise and experience we have and now stuck up to move further !
  • KrishnakantB
    var KiteConnect = require("kiteconnect").KiteConnect; I have install kiteconnectjs current version as per our above conversation. after install there is two folder node_module, pakage.json. I need to know require("kiteconnect") reference.
  • anirban1990
    anirban1990 edited March 23
    @KrishnakantB
    The kite connect js is basically a node js module, you wont require a node expertise to use that, simply install node and npm , it will download node_modules, then install kiteconnect using npm(npm install kiteconnect) install, this will download the kiteconnect module, all you need to do is create a js file name it server.js or anything. Add the below lines of code
    var socket  = require( 'socket.io' );
    var express = require('express');
    var app = express();
    var server = require('http').createServer(app);
    var io = socket.listen( server );
    var port = process.env.PORT || 3000;
    var KiteTicker = require("kiteconnect").KiteTicker;
    var ticker = new KiteTicker({
    api_key: 'api_key',
    access_token: 'access_token'
    });
    //ticker.autoReconnect(true, 10, 5)
    ticker.connect();
    ticker.on("connect", function(){
    var items = [128028676];
    ticker.subscribe(items);
    ticker.setMode(ticker.modeFull, items);
    });
    ticker.on("ticks", function(ticks){
    console.log("Ticks",ticks);
    });
    //Then go to command line and run node server.js you should be able to see the result. You can then connect with this node application using a curl call from your php code with port specifying as the port on which the node js is running in above example port is 3000, this will help you integrate with socket.io, you can even use elephant.io to achieve the same
Sign In or Register to comment.