Tuesday, 17 September 2013

Named events -- catch-all or specific?

Named events -- catch-all or specific?

I'm developing client- and server-apps (node.js, socket.io for
communication). Messages sent between the client and server have many
different types, and I'd originally thought to include the type in the
transmitted object:
// Client
var data = {
"type" : 5,
"message" : "Hello, world!"
};
socket.emit("generic_event", data);
On the server side, this would be handled as such:
// Server
socket.on("generic_event", function(e){
console.log("Got a generic_event!", e.type);
});
However, there is also the possibility of being more specific with event
types, e.g.:
// Client
socket.emit(data.type, data);
// Server
socket.on(1, function(){
console.log("We got '1'!");
});
// Elsewhere on in the server...
socket.on(5, function(){
console.log("We got '5'!");
});
I'm curious to know what the benefits are of following scheme A (single
generic / catch-all event) vs. scheme B (multiple/many specific named
events). Performance? Personal coding style? Something else?

No comments:

Post a Comment