Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js socket.io middleware

In node.js we can add some middleware function to socket.io server like this

 io.use(function(socket, next) {
            var req = socket.handshake;
            var res = {};
            cookieParser(req, res, function(err) {
                if (err) return next(err);
                session(req, res, next);
            });
        });

I wonder if we can add some kind of middleware to simple socket.on function as in the following example

io.sockets.on('connection', function(socket){
    socket.on('someSignal',
             function(data,next){/*middleware function code*/ next();},
             function(data){})
});
like image 298
Vladyslav Nikolaiev Avatar asked May 19 '26 08:05

Vladyslav Nikolaiev


1 Answers

emitter.on(event, listener) is the alias for emitter.addListener(event, listener). if you want to do this, every request pass through this middleware will add one eventListener.

like image 96
BenHu Avatar answered May 28 '26 12:05

BenHu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!