socket.io-client-dart icon indicating copy to clipboard operation
socket.io-client-dart copied to clipboard

Unable to stop automatic reconnection attempts

Open solofx opened this issue 8 months ago • 4 comments

I've got some code that establishes a connection to my socket.IO server, which works perfectly well and allows messages to transit back and forth. The connection is established with auto-reconnection enabled for fault tolerance.

void connectToSocket() {
  IO.Socket socket = IO.io(socketUrl,
    IO.OptionBuilder()
      .setTransports(['websocket'])
      .enableAutoConnect()
      .enableReconnection()
      .build()
  );

}

I have functionality within my application for the user to change the connection URL to switch servers. When this happens, I need to disconnect the existing connection, prevent any re-connection attempts and establish a replacement connection.

I've tried calling various methods to terminate the existing connection, however the reconnection attempts persist in the background, even once the replacement connection has been established. For example:

socket.disconnect();
socket.close();
socket.clearListeners();

connectToSocket();

So far the only way to effectively stop the connection attempts is to terminate the application, which seems a little extreme. Am I missing something or is this expected functionality? If so, does an approach exist to stop an automatically reconnecting socket instance?

My version of the socket.io client is "2.0.3"

solofx avatar Feb 26 '25 17:02 solofx