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

[feature] add a way to register middlewares for all namespaces #3851

Closed
darrachequesne opened this issue Mar 19, 2021 · 3 comments
Closed

[feature] add a way to register middlewares for all namespaces #3851

darrachequesne opened this issue Mar 19, 2021 · 3 comments
Labels
enhancement New feature or request

Comments

@darrachequesne
Copy link
Member

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

Since Socket.IO v3, it is not mandatory anymore for the client to connect to the main namespace.

Registering a global middleware which applies to all namespace is thus a bit messier:

const of = io.of;
io.of = (...args) => {
  const nsp = of.call(io, ...args);
  nsp.use(yourMiddleware);
  return nsp;
}

Describe the solution you'd like

io.<name of the method>(yourMiddleware); // register a middleware which will be attached to each namespace

Describe alternatives you've considered

Registering a global middleware is possible for dynamic namespaces:

const parentNamespace = io.of(/^\/dynamic-\d+$/);

parentNamespace.use(yourMiddleware); // attached to all child namespaces

Additional context

Related: #3842

@darrachequesne darrachequesne added the enhancement New feature or request label Mar 19, 2021
darrachequesne added a commit that referenced this issue May 10, 2021
A "new_namespace" event will be emitted when a new namespace is created:

```js
io.on("new_namespace", (namespace) => {
  // ...
});
```

This could be used for example for registering the same middleware for
each namespace.

See #3851
@stephaneeybert
Copy link

I also wanted to register a root main middlewaredid would fire up its handler for all namespaces.

I was trying to have the middleware:

io.use((socket, handler) => {
});

called up on a connection to:

server.io.of('/elearning').on('connection', function(socket) {
});

but the middleware handler would not fire up.

I also tried:

io.of('/').use((socket, handler) => {
});

but that also did not fire up.

Then I tried with a regex and that again did not fire up.

Only when I changed it to the same namespace is in the connection:

io.of('/elearning').use((socket, handler) => {
});

did it fire up.

But then I would need to have one same middleware for each and every namespace.

@sean256
Copy link

sean256 commented Jun 8, 2022

I'm glad I found this, I honestly thought io.use was bugged. v2 supported a global middleware and it was awesome.

darrachequesne added a commit to socketio/socket.io-website that referenced this issue Jun 27, 2022
@darrachequesne
Copy link
Member Author

This was added in the documentation: https://socket.io/how-to/register-a-global-middleware

I'm not sure whether we should add some syntactic sugar over this, so I'm closing this. Please reopen if needed!

dzad pushed a commit to dzad/socket.io that referenced this issue May 29, 2023
A "new_namespace" event will be emitted when a new namespace is created:

```js
io.on("new_namespace", (namespace) => {
  // ...
});
```

This could be used for example for registering the same middleware for
each namespace.

See socketio#3851
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants