engine.io icon indicating copy to clipboard operation
engine.io copied to clipboard

feature: `BaseServer.corsMiddleware` should be publicliy modifiable

Open salihguru opened this issue 3 years ago • 1 comments

Is your feature request related to a problem? Please describe.

socket.io (here engine.io) uses express's cors library for cors (dependency). I am currently developing a framework and had to write my own cors package in this framework. My framework supports websocket and uses socket.io. So when I try to use my cors package for websocket I do some very unpleasant things.

Describe the solution you'd like

The corsMiddleware function of the BaseServer class must be explicitly changed over the io instance or a middleware must be entered as a cors option.

Describe alternatives you've considered

Alternatively, the following option should be entered:

the current code is right here

    if (this.opts.corsMiddleware) {
       this.corsMiddleware = this.opts.corsMiddleware;
    } else if (this.opts.cors) {
      this.corsMiddleware = require("cors")(this.opts.cors);
    }

and here

    if (this.corsMiddleware) {
      const next = () => {
         this.verify(req, false, callback);
      }
      this.corsMiddleware(this.opts.cors)(req, res, next)
    } else {
      this.verify(req, false, callback);
    }

Although this is not a very good example, I think it is enough to explain my problem. I can create a better PR if you give ideas and support.

Best, Sami Salih İbrahimbaş

salihguru avatar Jul 17 '22 19:07 salihguru

Wow, sorry for skipping this, I'm currently using it like this

import { Server } from "socket.io"

const io = new Server();
io.engine.corsMiddleware = (opts, req, res, next) => {
    return myMiddleware(req, res, next)
}

salihguru avatar Jul 17 '22 19:07 salihguru

@ssibrahimbas Hi! Would adding the possibility to provide custom middlewares to the Engine.IO server suit your needs?

darrachequesne avatar Nov 20 '22 02:11 darrachequesne

Proper support for middlewares has been added in https://github.com/socketio/engine.io/commit/24786e77c5403b1c4b5a2bc84e2af06f9187f74a, included in [email protected] and [email protected]:

io.engine.use(yourMiddleware);

Reference: https://socket.io/docs/v4/middlewares/#compatibility-with-express-middleware

Please reopen if needed!

darrachequesne avatar Feb 08 '23 06:02 darrachequesne