Kite Connect Node JS library

AakashK
I am using kite connect node js library to get live data but it disconnecting after some time with error code 1006
  • AakashK
    AakashK edited March 2021


    This above image contain the issue as I mentioned
  • rakeshr
    @AakashK
    We ran nodejs Websocket at our end for more than 30 minutes, but we weren't able to reproduce above error. Maybe you can paste your Websocket code, so we can look at it.
  • AakashK

    const getStockDetails = async (io , socket) => {
    socket.on('getStockDetailsBySocket',function(tradingSymbol) {
    console.log('getStockDetails kite now :',tradingSymbol);
    // let stock_Name = stockName.replace(/_/g, " ");
    // console.log('stockName replace : ',stock_Name);
    stocksModel.find({"tradingsymbol":tradingSymbol},(error , stockData)=>{
    if(error)
    return res.json({ status : 400 , message : error.message })
    if (error)
    return reject({ status: 400 , message: error.message })
    for(let stock of stockData){
    stockArray = []
    stockArray.push(parseInt(stock.instrument_token));
    stockDetails[stock.instrument_token] = { symbol: stock.tradingsymbol, name: stock.name , exchange : stock.exchange }
    }

    if (!!tikerForStock)
    tikerForStock.disconnect();
    tikerForStock = new KiteTicker({
    api_key: process.env.KITE_API_KEY,
    access_token: kiteApiAccessToken,
    debug : true
    });
    tickerIntiateForStock(tikerForStock);
    // return res.json({ status : 200 , message : "Stock data retrived Successfully" , data : stock})
    })
    });
    }

    const tickerIntiateForStock = async (tikerForStock) => {
    // set autoreconnect with 10 maximum reconnections and 5 second interval
    if(!!tikerForStock)
    tikerForStock.disconnect();
    console.log('reinitiating tikerForStock')
    tikerForStock.autoReconnect(true, 10, 5)
    tikerForStock.connect();
    tikerForStock.on("ticks", onTicksForStock);
    tikerForStock.on("connect", subscribeStock);
    tikerForStock.on("noreconnect", function() {
    console.log("noreconnect");
    });
    tikerForStock.on("error", function(error) {
    console.log('stock ticker error',error);
    });
    tikerForStock.on("close", function() {
    console.log("stock ticker closed connection");
    });
    tikerForStock.on("disconnect", function(error) {
    console.log("stock ticker disconnecting with error");
    console.log(error);
    });
    tikerForStock.on("reconnect", function(reconnect_count, reconnect_interval) {
    console.log("Reconnecting: attempt stock ticker - ", reconnect_count, " interval - ", reconnect_interval);
    });
    }

    const onTicksForStock = (ticks) => {
    sendStockUpdates(ticks)
    }

    const subscribeStock = () => {
    var item = stockArray;//[738561];//instruments;//[738561];
    tikerForStock.subscribe(item);
    tikerForStock.setMode(tikerForStock.modeFull, item);
    }

    const sendStockUpdates = (ticks) => {
    ticks.forEach(update => {
    update.trading_symbol = stockDetails[update.instrument_token].symbol;
    update.name = stockDetails[update.instrument_token].name;
    update.exchange = stockDetails[update.instrument_token].exchange;
    try {
    socketIo.sockets.emit('liveStockUpdate', update);
    } catch (error) {
    console.log('liveStockUpdate Error :',error.message);
    }
    });
    }
  • AakashK
    @rakeshr please check my above code as you said to paste my code here
  • rakeshr
    @AakashK
    Looks like sendStockUpdates function call inside ticker, is blocking main ticker thread. You can go through this thread, to know more about this issue.
    closeEvent code:1006 is because of block of ticker thread.
Sign In or Register to comment.