under this section: https://socket.io/docs/v4/server-socket-instance/#socket-middlewares
while trying to run the sample code in my local machine, the provided sample simply does not works. below are the code used for testing:
namespaceSocket.on("connection", (socket) => {
console.log("a client has connected")
socket.use(([event, ...args], next) => {
if (true) {
return next(new Error("unauthorized event"));
}
// do not forget to call next
next();
});
socket.on("error", (err) => {
if (err && err.message === "unauthorized event") {
console.log("unauthorized")
socket.disconnect();
}
});
});
given the sample, I expected that all socket will trigger error and get disconnected whenever its connected, but the middleware section is ignored
after trying to debug it with a simpler sample
const log = (msg: string) => (socket, next) => {console.log(msg);next()}
namespaceSocket.use(log("namespace socket middleware")) //this is logged
namespaceSocket.on("connection", (socket) => {
socket.use(log("connected socket middleware")) // this is NOT logged
console.log("a user connected") //this is logged
});
the result is that the middleware outside of connections events does trigger but the middleware inside a successful connections is never triggered.
I also cant tell whether I have missed some configuration which made my local test not behave as documented, so i hope someone can gives me pointer on what is going on