From e57a4f9300d63ac800291123c56fd4cc5ea69869 Mon Sep 17 00:00:00 2001 From: Priyanshu Date: Thu, 11 Jun 2026 09:18:48 +0000 Subject: [PATCH] docs: add global middleware section to middlewares page (fixes #4609) --- docs/categories/02-Server/middlewares.md | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/docs/categories/02-Server/middlewares.md b/docs/categories/02-Server/middlewares.md index cbe95222..676bbaee 100644 --- a/docs/categories/02-Server/middlewares.md +++ b/docs/categories/02-Server/middlewares.md @@ -192,3 +192,28 @@ io.engine.use((req, res, next) => { } }); ``` + +## Applying middleware to all namespaces + +By default, a middleware registered with `io.use()` only applies to the main namespace (`/`). If you need a middleware to run for **all namespaces**, you can use the [`new_namespace`](/docs/v4/server-api/#event-new_namespace) event: + +```js +const myMiddleware = (socket, next) => { + // ... + next(); +}; + +io.use(myMiddleware); + +io.on("new_namespace", (namespace) => { + namespace.use(myMiddleware); +}); +``` + +:::caution + +Namespaces registered before the `new_namespace` listener is added will not be affected. + +::: + +For more details and additional patterns (manual registration, dynamic namespaces), see [How to register a global middleware](/how-to/register-a-global-middleware). \ No newline at end of file