Websocket script stopped suddenly

metalaman
I have created a python script to fetch real time data of 6000 instruments. I run the script at 9:15 in the morning and it gets automatically killed saying Connection is already closed., after about an hour.
Thinking, this might be because the main thread is taking too long to update the database, thereby closing the websocket connection, I create a thread in on_tick callback to upload the tick. Not sure if this is the correct implementation, but resolved Socket is already closed error.
Can I get more information on resubscribe method, and an example of on_error and on_reconnect callback to establish connection again.
  • sujith
    Hi @metalaman,
    You won't get data for 6000 instruments.
    You can only subscribe to 200 instruments with one websocket connection. With one Kite Connect app, you can open up to 3 connections. Please make sure, you are not abusing the system.
    By the error message, it seems like the connection is already closed. Kite Connect has the reconnection feature, you can use it. You can check out example.
  • sujith
    writing to the database and doing calculations in worker thread is a feasible approach, you can do that.
  • metalaman
    metalaman edited October 2017
    @sujith Thanks for the prompt and informative reply.
    Do I have to define on_reconnect callback, If yes, Can you give an example of on_reconnect callback, I couldn't find it on the link you provided.
    Also, the tick size I get is much greater than 200.
    Before implementing the worker thread solution I was facing maximum recurson depth exceeded error. Any ideas as to why that would happen.
  • metalaman
    Also, do I need to resubscribe after receiving every tick.
  • sujith
    @metalaman,
    Sometimes when there is an abrupt change in network connection or drop in the network connection. onError is not called at all for about a minute or more. It is the basic behavior of the Websockets.
    Hence Kite Connect provides a reconnection solution wherein clients don't have to do anything, Kite Connect will take care of reconnection. At your end, you just need to make sure you are not blocking main thread and keep processing ticks on the worker thread.
    The way it works is ticker keeps sending heartbeat signal every second even though a user has not subscribed to anything. We use that to monitor the health of connection. Lets us say that heartbeat ticks are not received for about 5 seconds we start reconnection.
    You don't have to subscribe for every tick. Once you subscribe you keep getting ticks.
    Each tick size is predefined,
    LTP tick is 8 bytes
    Quote tick is 44 bytes
    Full mode tick is 164 bytes.
    Indices tick is 28 bytes(full and quote mode )
    Each binary stream you get will contain multiple ticks and hence it can vary.
    You can take a look at documentation and you can go through _parse_binary method to know how parsing works.
    In the above link, there is ticker usage example, you can take a look at it.
    To enable reconnect, you just need to call this method,
    kws.enable_reconnect(reconnect_interval=5, reconnect_tries=50)
  • metalaman
    metalaman edited October 2017
    @sujith With threaded=True, my websocket is not able to subscribe the tokens. It gets closed and hence doesn't fire the on_connect callback. Can you please check my implementation below:

    def on_connect_1(ws):
    print "1-In on_connect"
    ws.subscribe(some_tokens)
    ws.set_mode(ws.MODE_QUOTE, some_tokens)

    def on_connect_2(ws):
    print "2-In on_connect"
    ws.subscribe(some_other_tokens)
    ws.set_mode(ws.MODE_QUOTE, some_other_tokens)

    kws1 = WebSocket(apikey, publicToken, userID)
    kws2 = WebSocket(apikey, publicToken, userID)

    print "WS_1"
    kws.on_connect = on_connect_1
    kws.enable_reconnect(reconnect_interval=2, reconnect_tries=100000000)
    kws.connect(disable_ssl_verification=True, threaded=True)

    print "WS_2"
    kws2.on_connect = on_connect_2
    kws2.enable_reconnect(reconnect_interval=2, reconnect_tries=100000000)
    kws2.connect(disable_ssl_verification=True, threaded=True)


    The above code only prints WS_1 and WS_2
    Shouldn't it also print 1-In on_connect and 2-In on_connect.
Sign In or Register to comment.