Websocket stops streaming frequently. Is there a way to catch an exception?

arnavsaxena
Our python program stops capturing the stream very frequently. 8 times since this morning (in 1.5 hours). The problem is that we don't catch an exception and program stops receiving ticks.

Is there a way to catch an exception specific to your API?
Tagged:
  • sujith
    Hi @arnavsaxena,
    Can you let us know what client are you using?
  • arnavsaxena
    Hi @sujith, We are using a python client.
  • sujith
    @arnavsaxena,
    Can you set on_error, on_close, on_error and add some logs and check?
    For more information check out python documentation here.
  • arnavsaxena
    @sujith Sayandeep is sending in a snippet of the garbage values that are being received. His post is under approval, maybe because we are sending some of the garbage values and methods to reproduce the error. Please check
  • deepcyan
    We've added an on_message callback which seems to return garbage values, such as below. Do advise.
    ...
    self.ws.on_message = on_message_cb
    ...
    ...
    def on_message(self, message, ws):
    logging.error("on_message callback called: " + message)

    ---
    2017-03-21 15:48:52,767 models.py Got tick LastPriceTick(1490091530 13408002 190)
    2017-03-21 15:48:52,773 models.py Got tick LastPriceTick(1490091530 13328898 78)
    2017-03-21 15:48:52,773 atrader.py on_message callback called: >��
    �K�����������K�
    �� `�
    ����
    ��
    ��r��S�����B
    �d'
    �,�
    �^�̀/��,�
    +�gk1�2�(�0zK/��/��/
    ".��.�K0
    �0C�0pK0�X0���e�K_}4lւw�e�j�W�e��e��e�,e Kd�Kc��fIKfN4fXKfbKf�����K�t~�@)�� ����y�$ ^�y
    V��"K���\�*�� 4�4[�vX4
    ��4*�����K ��,����",'9,�6w;��a�K����K�
    �}�Kq�NN18=cX;~K;y�;`,;BK;�;��;�K
  • Shaj
    Shaj edited March 2017
    I think you should use on_tick callback which will return a processed python dict.
  • sujith
    sujith edited March 2017
    Hi @deepcyan,
    Tick data you get is binary. It is parsed and sent back to users.
    You can use on_tick callback as mentioned by @Shaj.
  • arnavsaxena
    @sujith @Shaj on_tick is fine. We do get the tick data as a python dict. But the problem is that the ticks stop coming very frequently and currently without providing an exception, so we are unable to restart the program internally to continue receiving ticks (without daemonizing or any helper script).
    Our question is still the same - is there a way that we can catch an exception when the ticks stop abruptly from within the program? Or do we have to use an external daemonize/ helper script to restart the program when it stops?
  • sujith
    @arnavsaxena,
    Is on_error callback not getting called?
    Is on_close callback getting called every time ticker disconnected?
  • deepcyan
    @sujith

    Both are getting called, see two typical variants of errors.
    2017-03-23 09:53:26,100 atrader.py ERROR on_error callback called: [Errno 32] Broken pipe
    2017-03-23 09:53:26,101 atrader.py ERROR on_close callback called
    2017-03-23 09:57:22,255 atrader.py ERROR on_error callback called: [Errno 104] Connection reset by peer
    2017-03-23 09:57:22,256 atrader.py ERROR on_close callback called
    Even after executing the callbacks, we're unable to do
    ws.reconnect
    or
    ws.close
    followed by
    ws.connect
    . They respectively return the exception messages: connection is already open, and closed respectively, and our thread dies subsequently.

    Please advise.
  • arnavsaxena
    @sujith Please advise?
  • kivijoshi
    I was having same issue in the past days. websocket gets interrupted .
    What you guys can do is simply put websocket connection in while loop. So after disconnect it start all over again. Its simple hack but pretty effective
  • arnavsaxena
    @kivijoshi @sujith
    Yeah sure, the only problem is that it has to restart the connection which takes time (~30-40 seconds). So maybe if there was a way to catch an exception, it could reconnect faster (<5-10 seconds)
  • Vivek
    In KiteconnectJs client we have a reconnect mechanism which runs a timer to check interval between last heart beat or data received time to current time and if it exceeds X seconds then it internally reconnects - https://github.com/rainmattertech/kiteconnectjs#auto-re-connect-websocket-client

    We don't have this for Python client yet but you can try implementing on your own. Here is the code snippet which handles it - https://github.com/rainmattertech/kiteconnectjs/blob/master/lib/ticker.js#L278
  • joy
    @vivek what is the preferred reconnection method

    1. check interval between 2 hear beats (like u said) or
    2. when socket OnClosed/OnError method is called.

    I have implemented the later. what u folks recommend.
  • Vivek
    @joy Checking interval between 2 heart beat is the better approach here because when the client is not aware of the network disconnection it could take several seconds before onClose or onError method will be triggered.

    To simulate this you can run the WebSocket client and manually disconnect your network. So the better approach is checking two heartbeat interval since its guaranteed that you will get atleast one heartbeat per second if client is connected.
  • joy
    @vivek thx vivek.

    Yes, I have already tested it by manually switching off the router etc. and there is a lag.

    Since there is a heartbeat in every second, it makes sense to check that. Will modify the code accordingly.
  • joy
    @vivek thx vivek.

    Yes, I have already tested it by manually switching off the router etc. and there is a lag.

    Since there is a heartbeat in every second, it makes sense to check that. Will modify the code accordingly.
This discussion has been closed.