Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding data to a socket.io object

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?

like image 869
user2924127 Avatar asked Apr 20 '15 01:04

user2924127


2 Answers

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
  ...
like image 51
TeemuK Avatar answered Sep 21 '22 13:09

TeemuK


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

like image 31
Andrew Lavers Avatar answered Sep 21 '22 13:09

Andrew Lavers