FrOz FrOz - 2 months ago 11
Node.js Question

Socket.io - listener must be a function on socket.io event

I'm trying to rewrite my socket.io server in order to dodge the "callback hell" thing. Now I'm trying to listen for an event, and instead of passing an anonymus function as callback, I want to pass a function exported by a module.

Basically I want to do this

socket.on('joinChat', myFuncExportedByModule(/* stuff */));


Instead of this

socket.on('joinChat', function() {
/* stuff */
});


As soon as I tried to invoke myFuncExportedByModule, node sent me back an "TypeError: listener must be a function".

Any ideas?

Answer

I think the error is you're not passing a function, but executing it.

Try remove parenthesis:

socket.on('joinChat', myFuncExportedByModule);