Working on a web application which is built upon expressJS and Socket.io. In the following post I saw the usage of middleware syntax which was new to me. Here is an example of the syntax:
const io = require('socket.io')();
io.use(function(socket, next) {
// execute some code
next();
})
.on('connection', function(socket) {
// Connection now authenticated to receive further events
socket.on('message', function(message) {
io.emit('message', message);
});
});
It basically uses middleware functions on the socket.io
instance. My previous understanding was that middlware can only be used on the express instance (app.use(...)
).
Questions:
- Is this syntax just regular middleware which works similarly to
app.use(...)
? - If it is different, what are the differences?