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.
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
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.
"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-
@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.
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..
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)
@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
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 ?
Which Kite Connect client are you using?
It seems to be working fine. Can you paste your code?
https://kite.trade/forum/discussion/1364/websocket-stops-streaming-frequently-is-there-a-way-to-catch-an-exception
or this-
https://kite.trade/forum/discussion/927/issue-with-websocket-kws-on-tick-hangs
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.
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-
on_data
callback.But yeah in next release I will add this callback and can be used to receive raw data from sockets including heartbeats.
_on_data
method will be after modification Now in your code assign on_data callback and your callback method will be something like thiskws.on_message = on_message
instead of
kws.on_data = on_data ?
on_data
callback.