Callbacks don't work if broadcasted. #770

Closed
alexindigo opened this Issue Feb 29, 2012 · 2 comments

Comments

Projects
None yet
2 participants

When you do socket.broadcast.emit(..., callback) it stored in the socket object

  if ('function' == typeof lastArg) {
    packet.id = ++this.ackPackets;
    packet.ack = lastArg.length ? 'data' : true;
    this.acks[packet.id] = lastArg;
    args = args.slice(0, args.length - 1);
  }

and broadcasted packets don't know about it

    this.namespace.in(this.flags.room).except(this.id).packet(packet);

after callbacks come

      if (socket.acks[packet.ackId]) {
        socket.acks[packet.ackId].apply(socket, packet.args);
      } else {
        this.log.info('unknown ack packet');
      }

they just being dropped.

So quick&dirty fix it I ended up doing following:

socket.js:209

    this.namespace.in(this.flags.room).except(this.id).packet(packet, (packet.id in this.acks) ? this.acks[packet.id] : null);

namespace.js:147

SocketNamespace.prototype.packet = function (packet, callback) {
  packet.endpoint = this.name;

  // it's dirty hack but fast one issue github:#770
  if (callback && this.manager.rooms[this.flags.endpoint]) {
    for (var i=0, l=this.manager.rooms[this.flags.endpoint].length; i<l; i++) {
      var id = this.manager.rooms[this.flags.endpoint][i];
      if (!~this.flags.exceptions.indexOf(id)) {
        this.manager.namespaces[this.flags.endpoint].sockets[id].acks[packet.id] = callback;
      }
    }
  }

Let me know if you have any questions,
or want real pull request.

Thanx.

jcollum commented May 30, 2013

Well I'd like to see it in a pull request. I assumed a broadcast emit would have a callback, but apparently not. What did the owners say about it?

This issue was closed.

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