socketio client throws ConnectionError when can't connect to server (timeout or no route to host) instead of continuously trying to reconnect #871
-
I am trying to have a python client connect to a socket.io server that is resilient to network failure, but I am encountering an issue when the connection times out or when there is no route to host. I would expect the sio client to keep retrying to connect to the server indefinitely, but instead it throws an exception and stops. Here are the relevant tracebacks:
My code is effectively just: import socketio
sio = socketio.Client()
sio.connect("http://socket-server:3080")
# event handlers here Is the expected behavior? And if so, what should I do to reach my expected behavior? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The Socket.IO client does not retry the initial connection attempt. There is reconnection support, but that kicks in when a previously established connection is lost. For an initial connection, you can build a retry loop on your side. |
Beta Was this translation helpful? Give feedback.
-
Thanks! For anyone else with this issue this is my solution: sio = socketio.Client()
connected = False
while not connected:
try:
sio.connect("http://socket-server:3080")
print("Socket established")
connected = True
except Exception as ex:
print("Failed to establish initial connnection to server:", type(ex).__name__)
time.sleep(2) |
Beta Was this translation helpful? Give feedback.
The Socket.IO client does not retry the initial connection attempt. There is reconnection support, but that kicks in when a previously established connection is lost. For an initial connection, you can build a retry loop on your side.