Python WebSocket Auto-Reconnection After Login Failure

I keep getting disconnected from the WebSocket server due to login failures, and I need a solution to automatically reconnect and retry the login when this happens to maintain a stable connection. Have anyone experienced this before?

Best Answer

  • wasin.w
    wasin.w admin
    Answer ✓

    Hello @AmeliaCatherine

    ### Reconnection ###

    There is the reconnection logic example available on the market_price_rdpgw_service_discovery.py example as follows:

    def _on_close(self, ws, close_status_code, close_msg):
        """ Called when websocket is closed """
        self.web_socket_open = False
        self.logged_in = False
        print("WebSocket Closed for " + self.session_name)


        if not self.disconnected:
            print("Reconnect to the endpoint for " + self.session_name + " after 3 seconds... ")
            time.sleep(3)
            self.connect()

    However, the example is for illustrative purposes only, as mentioned in the disclaimer.

    #!/usr/bin/env python
    # |-----------------------------------------------------------------------------
    # |            This source code is provided under the Apache 2.0 license      --
    # |  and is provided AS IS with no warranty or guarantee of fit for purpose.  --
    # |                See the project's LICENSE.md for details.                  --
    # |            Copyright (C) 2018-2021 Refinitiv. All rights reserved.        --
    # |-----------------------------------------------------------------------------

    ### login failures ###

    Can you give me more detail about the login failures issue?

    • The login request and response error messages
    • Are you connecting to deployed ADS or RTO (on the cloud)?
    • Can you replicate the issue on demand?