socket.send() callbacks don't work #645

Closed
hjesbguhs opened this Issue Nov 15, 2011 · 3 comments

3 participants

@hjesbguhs

When using socket.send() instead of socket.emit() the callbacks don't work.

client:

socket.send('test',function(data){
...
});

server:

socket.on('message',function(data,cb){
//cb is undefined
});

I got it working with these changes. However I'm not quite sure if it's a bug or you just shouldn't use .send with callbacks.

node (in Socket.prototype.send) replace:

...
  if (fn) {
    packet.id = ++this.ackPackets;
    packet.ack = true;
    this.acks[packet.id] = fn;
  }
...

with

...
  if ('function' == typeof fn) {
    packet.id = ++this.ackPackets;
    packet.ack = fn.length ? 'data' : true;
    this.acks[packet.id] = fn;
  }
...

browser (in SocketNamespace.prototype.send):

...
    if ('function' == typeof fn) {
      packet.id = ++this.ackPackets;
      packet.ack = true;
      this.acks[packet.id] = fn;
    }
...

with

...
    if ('function' == typeof fn) {
      packet.id = ++this.ackPackets;
      packet.ack = fn.length ? 'data' : true;
      this.acks[packet.id] = fn;
    }
...
@steveluscher

I've noticed this too. I would like "data" type acknowledgements when using send() and json.send() but it seems that Socket.IO is designed to only support "data" type acknowledgements on emitted events.

@rauchg

@steveluscher You can simply emit('message') with a callback. This is what I'm doing for sio 1.0 =]

@steveluscher

Right, but if you don't need the overhead of emit() (which adheres to the {name: …, args: …} protocol, and simply want to use send() and sendJSON(), you can't take advantage of "data" type acknowledgements.

Also, if you use emit() on the client side, you have to explicitly listen for named events on the server side. If you use send() on the client side, you can catch all of the messages in your server's on('message', function() { … }).

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment