Why are no exceptions being thrown by kiteconnectjs?

Ismail
I am having trouble connecting to KiteTicker. The most basic code is not working ...

subscribe(instruments, onTicks) {
const _kt = this.kt = new KiteTicker({
api_key: 'xxx',
access_token: 'xxx'
})

_kt.connect()
console.log( _kt.connected() ) // gives me false

_kt.on('ticks', onTicks)
_kt.on('connect', () => {
_kt.subscribe(instruments)
_kt.setMode(_kt.modeFull, instruments)
}
}
I have checked the api_key and the access_token to be correct. So I opened the /node_modules/../ticker.js file in /node_modules to figure out what is the problem. To my surprise there is no error checking in the connect() function. If the connection is open connect() simply returns (without even a true/false).

API functions must validate the parameters - and throw meaningful exceptions. It is even more necessary for languages like javascript where there is no type checking and debugging server side code is time consuming. For now I have absolutely no clue why the websocket does not connect.
  • Ismail
    There is no bug in the above code, the connectivity should have been checked AFTER the subscription is done. Or inside the 'connect' message.
  • rakeshr
    @Ismail
    We tried exact same code as above at our end, after rectifying few syntax error and it's working fine.
    var KiteTicker = require("kiteconnect").KiteTicker;
    function subscribe(instruments, onTicks) {
    const _kt = this.kt = new KiteTicker({
    api_key: 'xxxxx',
    access_token: 'xxxxx',
    });

    _kt.connect();
    _kt.on('ticks', onTicks);
    _kt.on('connect', () => {
    _kt.subscribe(instruments);
    _kt.setMode(_kt.modeFull, instruments);}) }

    subscribe([779521],onTicks);

    function onTicks(ticks) {
    console.log("Ticks", ticks);
    }
    Response:
    [ { tradable: true,
    mode: 'full',
    instrument_token: 779521,
    last_price: 224.85,
    last_quantity: 1,
    average_price: 222.03,
    volume: 95983850,
    buy_quantity: 16886,
    sell_quantity: 0,
    ohlc: { open: 217, high: 225.9, low: 216.8, close: 215.65 },
    change: 4.266172038024571,
    last_trade_time: 2020-08-28T10:29:31.000Z,
    timestamp: 2020-08-28T12:11:05.000Z,
    oi: 0,
    oi_day_high: 0,
    oi_day_low: 0,
    depth: { buy: [Array], sell: [Array] } } ]
  • Ismail
    @rakeshr
    Thank you Rakesh, for the effort taken to go through my code. The error was in my array of instrument. It contained strings [ "9594882", ...] instead of integers [ 9594882, ...].

    Having said that, I would still want exceptions to be thrown (or promises to fail for asynchronous code) by the API when it detects an error. The KiteTicker methods should not fail silently. I hope we agree on this. :smiley:

  • sujith
    You can listen to error messages on the on_error callback.
Sign In or Register to comment.