Jacob Cohen
Jacob Cohen

Reputation: 1272

NodeJS Express Socket IO Routes & MVC

When working in HTTP protocol routing is quite easy. You're looking at the header, get the route, and then have a dictionary that defines which routes goes to which controller.

For example:

{
    "/"        : { "controller" : "MainController", "action" : "index", "di" : {} },
    "/Login"    : { "controller" : "LoginController", "action" : "login", "di" :  {"LoginService"} }
} 

I would like to try and match that behaviour when working with socket io on nodejs express.

The problem is, I don't know how to "listen" to any on and look at its properties as if it was an http request and I'm to check its headers.

So the big question is: How can I listen to ANY event?

Let's assume the following desired code:

io.on('connection', function(socket) {
    socket.any(Route, callback);
}

Where any is a made up function.

Upvotes: 1

Views: 1821

Answers (1)

James
James

Reputation: 82096

Socket.io supports a similar middleware framework like express, if you want to intercept every request then you can do:

io.use((socket, next) => {
   // inspect socket.handshake.headers
});

Upvotes: 1

Related Questions