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

后端 未结 2 1582
天涯浪人
天涯浪人 2021-02-13 14:07

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

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

Variables can also be set and accessed on the

2条回答
  •  被撕碎了的回忆
    2021-02-13 15:05

    I believe the primary reason is so the data attached to the socket is multi-process safe.

    If you're app is single process, always will be single process, and you're sure you're not overriding an internal attribute, socket.foo = bar will be fine. It would still be best to use get/set as a matter of future-proofing and best-practices.

    In a multi-process world, if you set socket.foo = bar in one process, then in another process socket.foo will be undefined.

提交回复
热议问题