An older question, but he came across a similar case, so I decided that I would share how to do it.
Client side
Therefore, instead of passing an event on the client, use the common name of the event, such as event or something else, as the first argument to emit. I ended up using the application name since it was short. Then in the data object, pass the name of the function you want to call.
So this is:
socket.emit('awesomeSocketEvent', {hello: 'world'});
It becomes:
socket.emit('genericEvent', {event:'awesomeSocketEvent', hello:'world'});
The rewriting of everything on the client side sucks, and my client stuff was basically just emit , so I wrapped the socket.io client in another object with its own emit event, which changed the event names and data objects.
Server side
Now on the server side there will always be only one custom event, genericEvent , so custom events should be restructured as properties of the object. Once this happens, this is a simple check to see if an event exists in the object, call it if it exists, or do something else if it isn't.
io.sockets.on('connection', function (socket) { var events = { awesomeSocketEvent: function(data){
After looking at the code while writing this part, I'm not sure about the performance with the fact that the event object is declared in the connect event. It might be better to declare events outside the connection event, and then pass socket to all of your functions.