Ticks not coming after reconnect

soumyadeep
Sometimes the websocket stops abruptly and then reconnects but then no ticks come after reconnection. I have been observing this for the last one week and I have to manually restart the websocket connection which is a pain as it requires me to be constantly sit in front of computer and observe if the websocket has stopped. Like today, the websocket stopped working after 12:15 and not a single data got stored.
  • sujith
    Hi @soumyadeep,
    Which Kite Connect client are you using?
  • soumyadeep
    pykiteconnect the latest one from github.
  • sujith
    Hi @soumyadeep,
    It seems to be working fine. Can you paste your code?
  • soumyadeep
    I think reading all your comments on other threads, I need to get the heartbeat in pykiteconnect and see if I do not get heartbeat let's say after 5 seconds, I will restart the connection automatically. Now if you could please help me with the python code to "see" the heartbeat that will be appreciated. Thanks
  • sujith
    Hi @soumyadeep,
    These are the threads before reconnection was implemented. Now we have implemented reconnection, you don't have to check for ticks for 5 seconds Python Kite Connect is already doing it for you.

    Once you connect to ticker you keep getting ticks of size 1-byte without even subscribing for tokens which are heartbeat ticks.
    You might have to download pykiteconnect repo locally and use it in your script and include log inside _on_data method.
  • soumyadeep
    soumyadeep edited August 2017
    "These are the threads before reconnection was implemented. Now we have implemented reconnection, you don't have to check for ticks for 5 seconds Python Kite Connect is already doing it for you."
    Sure the reconnection works well but after reconnection, I am not getting any ticks... So I wanted to know if there is a command that I can use in my websocket script to print the heart beat. I am using example script. What lines should I add to print the heartbeat on screen? Will this work-
    def on_message(beats, ws)
    .......(tab).........print beats
    .
    .
    .
    .
    .
    #assign callbacks
    kws.on_message= on_message

  • Vivek
    @soumyadeep Currently we are not exposing heartbeat in any callback so you might have to edit the Pykiteconnect code yourself and add on_data callback.

  • soumyadeep
    I can do that. But I was wondering what does "on_message" do then? Doesn’t it show us that the connection is active if it spits out something all the time? The moment it stops then it means that the heartbeat stops? Am I wrong? Because I do not want to change the real code..
  • Vivek
    @soumyadeep We don't have on_data callback in PyKiteconnect - https://github.com/rainmattertech/pykiteconnect/blob/master/kiteconnect/__init__.py#L881

    But yeah in next release I will add this callback and can be used to receive raw data from sockets including heartbeats.
  • soumyadeep
    soumyadeep edited August 2017
    Thanks for the guidance... Accordingly, I added the line "self.on_data = None" but it just behaves like a tick.. Only when there is a tick, I get a response. Do I need to add any line in the following?

    def _on_data(self, ws, data, resp_type, data_continue):
    """Receive raw data from WebSocket."""
    # Set last read time (Heartbeat is received every second)
    self._last_read_time = int(time.time())

    if self.on_tick:
    # If the message is binary, parse it and send it to the callback.
    if resp_type != 1 and len(data) > 4:
    self.on_tick(self._parse_binary(data), self)
  • Vivek
    @soumyadeep Yeah you need to call the callback if its assigned with the raw data. Here is how _on_data method will be after modification
    def _on_data(self, ws, data, resp_type, data_continue):
    """Receive raw data from WebSocket."""
    # Set last read time (Heartbeat is received every second)
    self._last_read_time = int(time.time())

    # Callback on data
    if self.on_data:
    self.on_data(data, resp_type, self)

    if self.on_tick:
    # If the message is binary, parse it and send it to the callback.
    if resp_type != 1 and len(data) > 4:
    self.on_tick(self._parse_binary(data), self)
    Now in your code assign on_data callback
    kws.on_message = on_message
    and your callback method will be something like this
    def on_message(data, response_type, ws):
    print(data, response_type)
  • soumyadeep
    Ahhh perfect.. Thanks.. I changed it accordingly. Should I write in my code assign on_data callback-
    kws.on_message = on_message
    instead of
    kws.on_data = on_data ?
  • Vivek
    Vivek edited August 2017
    @soumyadeep My bad, it should be assigned to on_data callback.
    kws.on_data = on_data

    def on_data(data, response_type, ws):
    print(data, response_type)
  • soumyadeep
    Excellent. works pretty well. Thanks Vivek.
This discussion has been closed.