Skip to content

Common authentication for all namespaces in socket.io 3 #3842

Answered by darrachequesne
AntonNeld asked this question in Q&A
Discussion options

You must be logged in to vote

Thanks for raising this issue, that's indeed a change from v2 to v3.

A possible workaround would be to override the io.of method:

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

Dynamic namespaces could work too, depending on your use case:

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

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

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
3 replies
@AntonNeld
Comment options

@darrachequesne
Comment options

@martinj
Comment options

Answer selected by darrachequesne
Comment options

You must be logged in to vote
2 replies
@darrachequesne
Comment options

@Fabioni
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants