Socket.io's examples all follow this pattern
io.sockets.on("connection", function(mySocket){
mySocket.on("my message", function(myData){
...
});
});
It seems to me that this would create a new callback function for every connection. Assuming that every socket responds to the message in the same way, wouldn't it be more memory efficient to define the handler once for all sockets like this:
function myMessageHandler(data){
...
}
io.sockets.on("connection", function(mySocket){
mySocket.on("my message", myMessageHandler);
});
or even this:
io.sockets.on("my message", function(mySocket, myData){
...
});
If so, why would Socket.io recommend a practice that wastes memory? Are we expected to want to keep stateful variables for the socket inside the "connection" callback's closure?