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

Event connect_error does not trigger when a middleware fails in the server

Open Velkamhell97 opened this issue 2 years ago • 3 comments

Hi, could you help me with the next issue, I have the following code in my server and client:

//server - javascript
this.io.of('/chat').use(isAuth).on('connection', (socket) => SocketsChat(socket, this.io));

//isAuth - javascript
export const isAuth = async (socket:Socket, next:SocketNext) => {
  const token = socket.handshake.auth.token;
  
  try {
    const authUser = await verifyJWTUser(token);
    const newToken = await generateJWT(authUser.id);
    
    socket.data = {authUser, newToken};
    next();
  } catch (error) {
    console.log('error: ', error);
    next(new Error('Unauthoraized'));
  }
}

//client - dart flutter
socket.on('connect_error', (e) async {
  print(e);
  socketError = 'Error from socket backend';
  socket.disconnect();
});

When i intentionally send a invalid token, the middleware fails and the server does not connect, but in the client this event is not listened, i try with the method socket.onConnectionError and neither, when i try the same with a javascript as client this works perfectly, can you check this please, thanks a lot!

Velkamhell97 avatar Mar 01 '22 01:03 Velkamhell97

Hey is there a solution for this yet? What's the progress so far?

CipherShi avatar Nov 16 '22 07:11 CipherShi

have the same problem, anyone have a solution?

hsnbsst avatar Feb 13 '23 12:02 hsnbsst

socket.on('error', (data) => print("error $data"));

when you use it like this, the message is captured

hsnbsst avatar Feb 13 '23 12:02 hsnbsst