NO_GUI
NO_GUI

Reputation: 464

Can you combine socket.io emit features?

I was trying to send coordinate data for a game by using the broadcast modifier and volatile modifier at the same time. Is that able to be done?

socket.broadcast.volatile.emit('coords', x, y);

Upvotes: 0

Views: 137

Answers (1)

jfriend00
jfriend00

Reputation: 707996

Yes, you can do that. All the socket.broadcast and socket.volatile properties do is execute a getter than sets the appropriate flag and then returns the socket object to allow you to chain them. Here's the code for creating those getters:

var flags = [
  'json',
  'volatile',
  'broadcast',
  'local'
];

flags.forEach(function(flag){
  Object.defineProperty(Socket.prototype, flag, {
    get: function() {
      this.flags[flag] = true;
      return this;
    }
  });
});

Then, when you call .emit() all the flags are passed on to the adapter that does the actual sending as you can see here. So, the adapter gets the emit() and gets the flags (both broadcast and volatile) to do accordingly.

Note that the main feature of volatile is if that a message is not queued if the client connection is not currently available (client is polling or client has lost the connection and may be reconnecting).

Upvotes: 1

Related Questions