Socket.IO namespace middleware works pretty similar to how ExpressJS middleware does. In fact, the Socket Object also contains a request and a response object that we can use to store other properties in the same manner as we do with ExpressJS middleware functions:
namespace.use((socket, next) => {
const req = socket.request
const res = socket.request.res
next()
})
Because ExpressJS middleware functions have the following signature:
const expressMiddleware = (request, response, next) => {
next()
}
We can safely execute the same function in a Socket.IO namespace middleware passing the necessary arguments:
root.use((socket, next) => {
const req = socket.request
const res = socket.request.res
expressMiddleware(req, res, next)
})
However, that doesn't mean that all ExpressJS middleware functions...