For a deeper look into our Elektron API, look into:

Overview |  Quickstart |  Documentation |  Downloads |  Tutorials |  Articles

question

Upvotes
Accepted
24 3 7 11

Python websocket auto-reconnection after the connection closes

Hi, I was running my python program to test the stability to get data through RTO(via internet).(it's not complete yet, still in development)

And on last Saturday, the Websocket just closed without any error.

So, my question is how do I write the program so that it re-connects after disconnection automatically?

And isn't run_forever() suppose to do that for me? Thank you.

python#technologyapiwebsocketsdisconnection
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hello @tobywong

Thank you for your participation in the forum. Is the reply below satisfactory in resolving your query?


If so please can you click the 'Accept' text next to the appropriate reply? This will guide all community members who have a similar question.

Thanks,


AHS

Hi @wasin.w Non of the answers are satisfactory unfortunately.

But I figured it out myself. Thanks

Please be informed that a reply has been verified as correct in answering the question, and has been marked as such.

Thanks,
AHS

Upvotes
Accepted
78.6k 248 52 74

@tobywong

You can check the market_price_rdpgw_service_discovery.py example on GitHub which demonstrates how to reconnect to the server.

    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.

 ALL EXAMPLE CODE IS PROVIDED ON AN “AS IS” AND “AS AVAILABLE” BASIS FOR ILLUSTRATIVE PURPOSES ONLY. REFINITIV MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED, AS TO THE OPERATION OF EXAMPLE CODE, OR THE INFORMATION, CONTENT OR MATERIALS USED IN CONNECTION WITH EXAMPLE CODE. YOU EXPRESSLY AGREE THAT YOUR USE OF EXAMPLE CODE IS AT YOUR SOLE RISK


icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
78.6k 248 52 74

@tobywong

Thank you for reaching out to us.

I assume that you are using the Python WebSocket Client library to establish a WebSocket connection to RTO.

You need to check if that Python WebSocket Client library can perform auto reconnection. Typically, I think that the Python WebSocket Client library may not support this. You need to check the document of that Python WebSocket Client library.

If the library doesn't suport reconnection, the application need to detect disconnections and has a logic to reconnect to RTO.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
24 3 7 11

@Jirapongse I'm running the websocket in one thread while doing something in another thread.

Do I just start a new websocket thread in the on_close function so that when it's closed it will reconnect.

But it's just one time? If it closes the second time it won't be able to auto reconnect.

And correct me if I'm wrong but I'm pretty sure I can't start the same thread twice once it's terminated unless I re-start the program.

But the whole point of this is automation. If I have to re-start the program manually it's missing the point. Thank you.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
24 3 7 11

@Jirapongse

After running the program for a few days, i got the following:

1693790097645.png

The following is my attempt to start a new thread in the on_close function:

1693790166502.png

But the "Connection is alive" stop after 20 seconds:


How should I go about reconnecting after disconnection automatically? Thank you.


1693790097645.png (8.6 KiB)
1693790166502.png (25.9 KiB)
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

For your reference about the the "Connection is alive" stop after 20 seconds:

1693790714087.png

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.