user3787706
user3787706

Reputation: 659

attaching data to sockets

i want to have some clients connect to a room with socket.io, then store their information into an object (like a user-list)

if they leave the channel, i want that entry to be auto-deleted.

how can i hook some "outside" information to a socket? is it possible?

example:

user named "joe" connects

socket.emit('joinRoom', {username: 'joe'});

on the server, i want to do something like

socket.on('joinRoom', function(msg) {
    userData.push(msg.username);    //  <-- how can i simplify/automatize this?
}

is there something built-in to manage users?

(the problem arises from me wanting to hook passport-user information to sockets. joe is logged in with passport, the server reqknows that. but the socket doesn't, because there is no req at all)

eventually, i want to be able to say "send a socket message to the user that is logged in as joe". if joe leaves the channel, remove him from the userlist etc

is that possible? how would you do it? is my approach wrong? thanks

Upvotes: 0

Views: 801

Answers (1)

David Perez
David Perez

Reputation: 336

You can add properties to the socket object:

socket.on('joinRoom', function(msg) {
socket.username=msg.username;   
}

If the socket you want modify is not the transmitter you can do it through

io.sockets.connected[socket.id].username=msg.username

but you will need his id.

Upvotes: 1

Related Questions