54

Recently I have been messing around with socket.io and found this interesting thing, that I can have emit function callback like this.

I start emitting on client side like this:

client.emit('eventToEmit', dataToEmit, function(error, message){
    console.log(error);
    console.log(message);
});

Then I can fire a callback from server-side like this:

client.on('eventToEmit', function(data, callback){
    console.log(data);
    callback('error', 'message');
});

Everything works fine with no errors, but I am interested if doing something like this is appropriate since I have not seen anything similar in the documentation or any example so far.

antanas_sepikas
  • 5,644
  • 4
  • 36
  • 67
  • Also do note that acknowledgement functions can only be called once. – hexacyanide Dec 02 '13 at 23:30
  • 3
    And also note, that there's no "timeout" feature, so be careful when your code depends on calling acknowledgement function. Also if you are interested, please check my module I've create as workaround of this problem https://www.npmjs.com/package/timeout-callback – JakubKnejzlik Feb 17 '15 at 14:06
  • 1
    @JakubKnejzlik Could you add that module to github and give it a License, so it can be used? – Florian Wendelborn Jun 10 '15 at 16:23
  • 3
    @Dodekeract glad to hear someone would like to use it. Done :) – JakubKnejzlik Jun 11 '15 at 09:38

2 Answers2

56

It's perfectly legal.

Those callbacks are called 'acknowledgement functions' and are summarily mentioned in the Wiki and described a bit more in detail on the NPM page ('Getting acknowledgements').

EDIT: more recent documentation can be found here.

robertklep
  • 198,204
  • 35
  • 394
  • 381
  • 5
    No mention of the word "acknowledgement" on either of those links :( Have these gone away? Or been hidden? – Alex McMillan Apr 15 '15 at 02:39
  • Maybe the post is just too old, as none of the links provide the answer, the last one is even broken. – arnold Oct 29 '15 at 14:45
  • 1
    @arnold the `socket.io` website seems to be offline atm. – robertklep Oct 29 '15 at 14:54
  • 2
    I've been trying to figure this out with no luck. My code looks exactly like the server code on the socket.io website and it simply doesn't work. I'm still trying to figure out what is passed as the second argument, but it's definitely not a callback because trying to use it as one crashes the server. – drekka Dec 21 '15 at 07:03
  • @drekka for acknowledgements to work, the emitter-side needs to pass a function too: `socket.emit('message', data, function() { ... })`. The receiver will then get a second argument in their message handler: `socket.on('message', function(data, fn) { ... })`. – robertklep Dec 21 '15 at 08:45
  • 1
    Hmm. Okies. I'm using the socket.io iOS client library so I'm not sure if I'm doing things rights. I'm trying to send a message to the server and get an immediate response. Does anyone know of any examples on the inter webs that use both sever node code and iOS client? – drekka Dec 21 '15 at 12:10
  • @drekka perhaps start a new question? – robertklep Dec 21 '15 at 13:10
  • @drekka: are you using Flask? We had some problems with Flask's socket.io library, which only accepts websocket-only connections due to an internal issue. As of now, the current stable is still infected I think. Besides that: afaik the acknowledgements don't have an explicit error variable that is null or not to signal errors. You have to deal with errors via the returned data. – Bouncner Jul 18 '16 at 06:49
1

According to the socket.emit() documentation, the acknowledgement functions (callbacks) must be the last parameters to the socket.emit() call. I was running into an issue where the callback was null in the server code.
Make sure the callback(s) is/are the last argument to socket.emit()

e.g.

// correct
socket.emit('eventname', arg1, arg2, callback);

// incorrect
socket.emit('eventname', arg1, callback, arg2);