Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io, difference between socket.set() and socket property?

Socket.io recommends settings per-socket variables like so:

socket.set('foo', bar, function () {});

Variables can also be set and accessed on the socket:

socket.foo = bar

Is there a benefit to using the provided set() function?

like image 834
knite Avatar asked Feb 12 '12 18:02

knite


People also ask

What is the difference between Socket.IO and socket IO client?

socket-io. client is the code for the client-side implementation of socket.io. That code may be used either by a browser client or by a server process that is initiating a socket.io connection to some other server (thus playing the client-side role in a socket.io connection).

Is Socket.IO full duplex?

info. WebSocket is a communication protocol which provides a full-duplex and low-latency channel between the server and the browser. More information can be found here.

Does Socket.IO use WSS?

Note: You can use either https or wss (respectively, http or ws ).

Is Socket.IO difficult?

As explained above, getting started with Socket.IO is relatively simple – all you need is a Node. js server to run it on. If you want to get started with a realtime app for a limited number of users, Socket.IO is a good option. Problems come when working at scale.


1 Answers

Calling socket.foo sets your property on the socket object itself. This isn't recommended because you could be overriding an internal property that socket uses and depends upon. When you call socket.set() this is stored in an internal data structure that won't clash with internal properties.

https://github.com/LearnBoost/socket.io/blob/master/lib/socket.js#L246

Socket.prototype.set = function (key, value, fn) {
  this.store.set(key, value, fn);
  return this;
};
like image 172
JohnP Avatar answered Nov 13 '22 04:11

JohnP