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?
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).
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.
Note: You can use either https or wss (respectively, http or ws ).
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.
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;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With