All Socket.io examples follow this pattern.
io.sockets.on("connection", function(mySocket){ mySocket.on("my message", function(myData){ ... }); });
It seems to me that this will create a new callback function for each connection. Assuming each socket responds to a message in the same way, it would not be more efficient to use memory to define a 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 does Socket.io recommend a practice that takes memory? Are we expected to save state variables for the socket inside the connection callback closure?
source share