How to get multiple socket attributes in socket.io?

I am creating a chat application for which for each client connection I need to set some attributes for each client instance using socket.io. When I save the attribute, I use:

client.set('name', name, function () {}); client.set('email', email, function () {}); .... 

and it works fine. When I need to get all the properties of the client, I did not find a better way than this:

 client.get("name",function(err,name) { client.get("email",function(err,email) { ....... } } 

I need to enclose all "get" for asynchronous data retrieval; but if I had 10 properties, do I need to invest all 10 items? There must be a better way to do this, can someone help me?

+4
source share
1 answer

I do not attach attributes to the socket.

  io.sockets.on('connection', function(socket) { var username = "xx"; var email = "xx"; socket.on('doX', function(data) { socket.emit('ackX', {username: username, email: email}); }); }); 

I do not know if this is the best solution, but I have seen many such examples.

EDIT: socket.io - getting more than one field for a socket? The correct answer may suit your needs.

+2
source

Source: https://habr.com/ru/post/1480876/


All Articles