node.js - How to store client associated data in socket.io 1.0 -


the docs socket.io doesn't support .get .set now

is okay store client associated data

io.sockets.on('connection', function (client) {     client.on('data', function (somedata) {                     client['data'] = somedata;     });     }); 

in case need multiple nodes?

yes, ok add properties socket.io socket object. should careful not use names conflict built-in properties or methods (i'd suggest adding leading underscore or namescoping them sort of name prefix). socket javascript object , you're free add properties long don't cause conflict existing properties.

there other ways use socket.id key own data structure.

var currentconnections = {}; io.sockets.on('connection', function (client) {     currentconnections[client.id] = {socket: client};     client.on('data', function (somedata) {           currentconnections[client.id].data = somedata;      });         client.on('disconnect', function() {         delete currentconnections[client.id];     }); }); 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -