javascript - Sending array from server to client with socket.io -


i developing small chat app, , able display list of connected users. so, on server side, have:

var users = {};      io.on('connection', function(socket){         socket.on('newuser', function(pseudo) {             socket.pseudo = pseudo;             users[socket.pseudo] = socket;             io.sockets.emit('newuser', pseudo);             console.log(object.keys(users));         }); 

the console sends me [ 'root' ] if connect user root. , have on client side:

var pseudo = '#{user.username}'; socket.emit('newuser', pseudo);      socket.on('newuser', function(pseudo) {         $('#messagezone').append('<p><em>' + pseudo + ' joined conversation !</em></p>');     }); 

so question is, how can send full updated users array each client when connect , display in div?

thank help!

if want send current connected users other users server, this:

io.sockets.emit('allusers', object.keys(users)); 

and, in client set list div:

socket.on('allusers', function(listofusers) {     $('#allusers').html(listofusers.join(", ")); }); 

this list of keys users object looks pseudo values each user. array turns json, sent each client. each socket.io client parse json array , you'd receive array on client put html.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -