rikulo / socket.io-client-dart

socket.io-client-dart: Dartlang port of socket.io-client https://github.com/socketio/socket.io-client
https://quire.io
MIT License
669 stars 181 forks source link

dispose() and clearListeners() not working #267

Open ElegantSoft opened 2 years ago

ElegantSoft commented 2 years ago

Hello, I am using socket.io and when user logout I call

  void logout() {
    debugPrint("will logout socket");
    chatSocket?.clearListeners();
    chatSocket?.dispose();
  }

I use websocketService class and use it with getIt

class WebsocketService {
  // final Socket? socket;
  Socket? chatSocket;

  WebsocketService()
      : chatSocket =
            io("$Server/${SocketConstants.ChatNameSpace}", <String, dynamic>{
          'transports': <String>['websocket'],
          'query': <String, dynamic>{
            "access_token": token,
          }
        });

  void logout() {
    debugPrint("will logout socket");
    chatSocket?.clearListeners();
    chatSocket?.dispose();
  }

}

after calling logout function the app keeps handle sockets and doesn't clear listeners

jumperchen commented 2 years ago

which version are you using?

ElegantSoft commented 2 years ago

@jumperchen The latest version After debugging I found the issue was I create the connection 2 times so when I close connection it closes 1 connection. you must handle that.

ljmatan commented 2 years ago

Please see https://github.com/rikulo/socket.io-client-dart/issues/258#issuecomment-1049585853

hatemragab commented 1 year ago

Any updates i faces the same in version 2.0.0

iUngerTime commented 3 months ago

For anyone looking at this, there is a flaw in the design of the baseline io command. See the comment below.

image

When logging out, even when you clear and dispose of the listeners and socket, the underlying library keeps a cache of the instance you connected to. So the other parts of your query are pretty much ignored.

You need to use the .enableForceNew() on the connect, as you can see below in my code, or clear the cache upon logout.

image

or

image

PhantomKnight287 commented 2 months ago

For anyone looking at this, there is a flaw in the design of the baseline io command. See the comment below.

image

When logging out, even when you clear and dispose of the listeners and socket, the underlying library keeps a cache of the instance you connected to. So the other parts of your query are pretty much ignored.

You need to use the .enableForceNew() on the connect, as you can see below in my code, or clear the cache upon logout.

image

or

image

You are a life saver, thank you for this response. My backend was getting same query params again and again on each connection causing weird behaviour. This fixes the issue