socket.io callback is not being called
Problem
Doing some socket.io work. Seems like my emit Callback is not being fired. Am I doing it right?
this.logger.debug("joining room " + roomId);
callback = function() {
return this.logger.debug('finished emitting join for socket');
};
// this.config.ioTypes.join resolves to "j"
this.socket.emit(this.config.ioTypes.join, {
user: this.config.user,
room: roomId
}, callback);
My logging looks correct (note that the code above doesn't include everything that would generate these logs):
DEBUG: joining room 1245
And that's the end. It looks like socket.io is not calling my callback. But the docs are pretty clear:
Sometimes, you might want to get a callback when the client confirmed the message reception.
To do this, simply pass a function as the last parameter of
.send
or.emit
. What's more, when you use.emit
, the acknowledgement is done by you, which means you can also pass data along:
Am I doing something wrong here?
Solution
Figured it out on my own. Gonna leave this here because the docs aren't very clear on this one.
I used util.inspect(arguments)
to see what was coming in to my on
handler on the server side (coffeescript):
@logger.debug "onJoin args: " + util.inspect(arguments)
Which resulted in: ... '2': [Function: ack]
which looked like what I needed. So I added that to the signature of the on
handler. Then I added callback() if callback?
to the end of the handler. Looks good so far.
Discussion
View additional discussion.