How to send keealive for python websocket api

I have a python websocket listening to streaming data from TREP server which disconnect roughly every half minutes which seems indicates the connection is cut by server periodically, is there any keepalive message that I have to send to TREP server to avoid this? thanks

Best Answer

  • Hello @lzhuang1

    Your applications must be prepared to respond with a Pong message whenever they receive a Ping message from ADS(one component of TREP). Otherwise, the ADS will disconnect the application after PingTimeout which you can find in Login response. For more details, please refer to Ping and Pong web socket API document

    The snipped example source code:

    image

    For the complete source source code, please refer to market_price.py in WebSocket API Sample Applications which can be downloaded from Download of WebSocket API.

Answers

  • Here is part the request I sent (I cannot send a full list of rics because the comment length limit):

    {"Domain":"MarketPrice","ID":-500000,"Key":{"Name":["GBRPIZ7Y=","CAD="],"Service":"IDN_SELECTFEED"},"Priority":{"Class":1,"Count":1},"Streaming":true,"Type":"Request"}

  • @lzhuang1

    Not sure that Did you received ping message from ADS? and Have you sent pong back?

    Ping and Pong messages are exchanged between endpoints of a connection to verify that the remote endpoint is still alive. The ADS will send Ping messages to applications when it does not receive traffic for a while, so applications must be prepared to respond with a Pong message whenever they receive a Ping.

    The ADS includes an informational PingTimeout element in its Login response, indicating the time (in seconds) after which the ADS will disconnect the application if it receives no traffic in response to a sent Ping.

  • @pimchaya, thanks for your response.

    Yes I followed the example and that's what I did in my code, and I use websock-client 0.56.0 to connect the websocket api. The periodical disconnection decision seems from server side because the error shows "Connection is already closed" when my script is still listening

  • Hi @lzhuang1, before we go on investigating the protocol and ADS logs, can you please run the Python Websocket sample - market_price.py, included in the Websocket SDK, and confirm that this happens with the official sample as well?

  • Yes that's what did, I copy the script and the only thing I change is the login, app_id and the request. The request contains 359 RICs for streaming data. I cannot paste the RICs here because the comment size limit, let me know if I can reach out you and I can send them to you maybe

  • Can you please upload your complete code here. You can remove the credentials from the file, and we will test it with ADS here. Use "Upload File" button.