I have a similar issue to a previous question (Adding data to a socket.io socket object). I am using socket.io 1.3.4 and would like to add additional data to a socket when it connects. Socket.io used to have a method called set which allowed for this, but it seems to longer work. Is there a new way to do this now?
The correct way to do it as of v4 is to use data
property:
this.ioServer.on('connection', async (socket) => {
const user: IUser = socket.handshake.auth.user
socket.data.user = user
...
These get/set methods appear to have been removed for 1.0:
http://socket.io/blog/introducing-socket-io-1-0/#scalability
I think the new practice is to simply set properties on the socket object directly as suggested in the question you linked.
You can see an example of this in socket.io's chat example:
https://github.com/Automattic/socket.io/blob/318d62/examples/chat/index.js#L36
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