Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Middlewares not executed when setting up with useSocketServer #261

Closed
sittingbool opened this issue May 25, 2021 · 10 comments · Fixed by #511
Closed

fix: Middlewares not executed when setting up with useSocketServer #261

sittingbool opened this issue May 25, 2021 · 10 comments · Fixed by #511
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.

Comments

@sittingbool
Copy link

Description

Hi, I wanted to use a middleware for doing authentication.

The problem is: The middleware is never ever being called.

Following setup:

const io = require('socket.io')(server, {
      cors: {
          origin: '*', // ['http://localhost:4200', 'http://localhost:63342'],
          credentials: true
      }
  });

  useSocketServer(io, {
      controllers: [__dirname + '/controller/socket/*.js'],
      middlewares: [SocketAuthenticationMiddleware]
  });
@Middleware()
export class SocketAuthenticationMiddleware implements MiddlewareInterface {

    use(socket: Socket, next: express.NextFunction): void {
        const token = <string>socket.request.headers['x-api-token']; // break point here -> never called
        if(stringIsEmpty(token)) {
            console.error('denied connection due to  missing token ' + token);
            next(new Error(UserErrors.tokenInvalid));
        }
        console.log('Successfully connected socket for ' + token);
        next();
    }
}
@SocketController('/chat')
export class ChatController extends RootController {
    @OnConnect()
    connection(@ConnectedSocket() socket: Socket): void {
        console.log('client connected to chat: ' + socket.id); // <<< this one is called despite missing token
    }

Expected behavior

Middleware code being executed

Actual behavior

Middleware code not being executed

@sittingbool sittingbool added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels May 25, 2021
@sittingbool sittingbool changed the title fix: <your-title-goes-here> fix: Middlewares not executed when setting up with useSocketServer May 25, 2021
@sittingbool sittingbool changed the title fix: Middlewares not executed when setting up with useSocketServer fix: Middlewares not executed when setting up with useSocketServer May 25, 2021
@yakovets-evgeny
Copy link

I have a similar problem

@ltrillaud
Copy link

Me too

@sittingbool
Copy link
Author

This is now almost a year old. I am having this again in another project. Using socket.io 4. Ist this project still being maintained?

@attilaorosz
Copy link
Member

Yes, but unfortunately the time we can allocate is limited. I’m planning some refactoring and support for latest socket.io in the near future.

@sittingbool
Copy link
Author

I am doing some research currently.

My setup: Node LTS, express server.

The problem is located here:

middleware.instance.use(socket, next);

The middleware is not called, because the socket.io middleware in which it is wrapped is not called. So this might be a problem with the combination socket.io on an express server. I will dig deeper.

@sittingbool
Copy link
Author

One other suggestion: I am using a custom namespace. It seems like the middleware is namespace-specific:

socketio/socket.io#3082

@sittingbool
Copy link
Author

Confirmed last statement:

Tested with code like this:

    io.of('/my-namespace')
        .use((socket, next) => {
            console.log('middleware running with namespace...');
            next();
        });


    io.use((socket, next) => {
        console.log('middleware running in general...');
        next();
    });

    useSocketServer(io, {
        controllers: [__dirname + '/controllers/*.ctrl.js'],
        middlewares: [__dirname + '/middlewares/*.mw.js']
    });

leads to following output:

Listening on port 3000
Database loaded
middleware running with namespace...

@sittingbool
Copy link
Author

I'll propose a solution in a PR

@attilaorosz
Copy link
Member

Closing as this has been resolved in #511

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.
4 participants