1"use strict";(self.webpackChunksample_website=self.webpackChunksample_website||[]).push([[5353],{3905:(e,t,n)=>{n.d(t,{Zo:()=>m,kt:()=>g});var a=n(7294);function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function o(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,a)}return n}function l(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?o(Object(n),!0).forEach((function(t){r(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):o(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function i(e,t){if(null==e)return{};var n,a,r=function(e,t){if(null==e)return{};var n,a,r={},o=Object.keys(e);for(a=0;a<o.length;a++)n=o[a],t.indexOf(n)>=0||(r[n]=e[n]);return r}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(a=0;a<o.length;a++)n=o[a],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(r[n]=e[n])}return r}var c=a.createContext({}),s=function(e){var t=a.useContext(c),n=t;return e&&(n="function"==typeof e?e(t):l(l({},t),e)),n},m=function(e){var t=s(e.components);return a.createElement(c.Provider,{value:t},e.children)},p="mdxType",d={inlineCode:"code",wrapper:function(e){var t=e.children;return a.createElement(a.Fragment,{},t)}},u=a.forwardRef((function(e,t){var n=e.components,r=e.mdxType,o=e.originalType,c=e.parentName,m=i(e,["components","mdxType","originalType","parentName"]),p=s(n),u=r,g=p["".concat(c,".").concat(u)]||p[u]||d[u]||o;return n?a.createElement(g,l(l({ref:t},m),{},{components:n})):a.createElement(g,l({ref:t},m))}));function g(e,t){var n=arguments,r=t&&t.mdxType;if("string"==typeof e||r){var o=n.length,l=new Array(o);l[0]=u;var i={};for(var c in t)hasOwnProperty.call(t,c)&&(i[c]=t[c]);i.originalType=e,i[p]="string"==typeof e?e:r,l[1]=i;for(var s=2;s<o;s++)l[s]=n[s];return a.createElement.apply(null,l)}return a.createElement.apply(null,n)}u.displayName="MDXCreateElement"},3269:(e,t,n)=>{n.r(t),n.d(t,{contentTitle:()=>l,default:()=>p,frontMatter:()=>o,metadata:()=>i,toc:()=>c});var a=n(3117),r=(n(7294),n(3905));const o={title:"How to register a global middleware"},l="How to register a global middleware",i={type:"mdx",permalink:"/how-to/register-a-global-middleware",source:"@site/src/pages/how-to/register-a-global-middleware.md",title:"How to register a global middleware",description:"In Socket.IO v2, a middleware that was registered for the main namespace would act as a global middleware, applying to all namespaces, since a client would first connect to the main namespace, and then to the custom namespace:",frontMatter:{title:"How to register a global middleware"}},c=[],s={toc:c},m="wrapper";function p(e){let{components:t,...n}=e;return(0,r.kt)(m,(0,a.Z)({},s,n,{components:t,mdxType:"MDXLayout"}),(0,r.kt)("h1",{id:"how-to-register-a-global-middleware"},"How to register a global middleware"),(0,r.kt)("p",null,"In Socket.IO v2, a middleware that was registered for the main namespace would act as a global middleware, applying to all namespaces, since a client would first connect to the main namespace, and then to the custom namespace:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-js"},'// Socket.IO v2\n\nio.use((socket, next) => {\n // always triggered, even if the client tries to reach the custom namespace\n next();\n});\n\nio.of("/my-custom-namespace").use((socket, next) => {\n // triggered after the global middleware\n next();\n});\n')),(0,r.kt)("p",null,"This is not the case any more starting with Socket.IO v3: a middleware attached to the main namespace is only called when a client tries to reach the main namespace:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-js"},'// Socket.IO v3 and above\n\nio.use((socket, next) => {\n // only triggered when the client tries to reach the main namespace\n next();\n});\n\nio.of("/my-custom-namespace").use((socket, next) => {\n // only triggered when the client tries to reach this custom namespace\n next();\n});\n')),(0,r.kt)("p",null,"To create a global middleware in newer versions, you need to attach your middleware to all namespaces:"),(0,r.kt)("ul",null,(0,r.kt)("li",{parentName:"ul"},"either manually:")),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-js"},'const myGlobalMiddleware = (socket, next) => {\n next();\n}\n\nio.use(myGlobalMiddleware);\nio.of("/my-custom-namespace").use(myGlobalMiddleware);\n')),(0,r.kt)("ul",null,(0,r.kt)("li",{parentName:"ul"},"or by using the ",(0,r.kt)("a",{parentName:"li",href:"/docs/v4/server-api/#event-new_namespace"},(0,r.kt)("inlineCode",{parentName:"a"},"new_namespace"))," event:")),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-js"},'const myGlobalMiddleware = (socket, next) => {\n next();\n}\n\nio.use(myGlobalMiddleware);
1\n\nio.on("new_namespace", (namespace) => {\n namespace.use(myGlobalMiddleware);\n});\n\n// and then declare the namespaces\nio.of("/my-custom-namespace");\n')),(0,r.kt)("admonition",{type:"caution"},(0,r.kt)("p",{parentName:"admonition"},"The namespaces that are registered before the ",(0,r.kt)("inlineCode",{parentName:"p"},"new_namespace")," listener won't be affected.")),(0,r.kt)("ul",null,(0,r.kt)("li",{parentName:"ul"},"or, when using ",(0,r.kt)("a",{parentName:"li",href:"/docs/v4/namespaces/#dynamic-namespaces"},"dynamic namespaces"),", by registering a middleware on the parent namespace:")),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-js"},"const myGlobalMiddleware = (socket, next) => {\n next();\n}\n\nio.use(myGlobalMiddleware);\n\nconst parentNamespace = io.of(/^\\/dynamic-\\d+$/);\n\nparentNamespace.use(myGlobalMiddleware);\n")),(0,r.kt)("admonition",{type:"caution"},(0,r.kt)("p",{parentName:"admonition"},"Existing namespaces always have priority over dynamic namespaces. For example:"),(0,r.kt)("pre",{parentName:"admonition"},(0,r.kt)("code",{parentName:"pre",className:"language-js"},'io.of("/admin");\n\nio.of(/.*/).use((socket, next) => {\n // won\'t be called for the main namespace nor for the "/admin" namespace\n next();\n});\n'))),(0,r.kt)("p",null,"Related:"),(0,r.kt)("ul",null,(0,r.kt)("li",{parentName:"ul"},(0,r.kt)("a",{parentName:"li",href:"/docs/v4/middlewares/"},"Middleware documentation")),(0,r.kt)("li",{parentName:"ul"},(0,r.kt)("a",{parentName:"li",href:"/docs/v4/migrating-from-2-x-to-3-0/#no-more-implicit-connection-to-the-default-namespace"},"v2 to v3 migration"))))}p.isMDXComponent=!0}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.